Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Filter content by current user

Viewing 6 posts - 1 through 6 (of 6 total)
  • Georg
    #134903

    Hi Trevor,

    is it possible to display only posts generated by the current user?

    I set up a custom post type (user_notes) and a front end form to allow users to generate their own user notes. Is ist possible to add a filter to the user notes page that shows only content generated by the current user?

    Couldn’t find anything like that in Post Meta.

    Thanks a lot, regards

    Georg

    Trevor Moderator
    #134914

    IF it could be done, it would very very difficult. My guess is that you would have to use the Edit Query Arguments Filter.

    The big issue would be security. How to stop someone spoofing the url query? Not something I could advise on.

    Georg
    #135093

    Hi Trevor,

    thanks for pointing me to Edit Query Arguments Filter, must have missed that!

    Here is how I set things up:
    – a front end form for submitting private custom posts (cpt supports author)
    – search & filter restricted to
    – post type: my custom post type
    – post status: private

    I expected search & filter to take into account that limitation to private actually limits search results to current users posts, as a regular wordpress query would do. But maybe my setup is wrong.

    If s&f doesn’t work this way, consider this a feature request. Because otherwise it renders wordpress post status private useless.

    Next, I tried your approach of editing Arguments Filter, but it gives me no results. Probably wrong syntax?

    
    function filter_user_posts( $query_args, $sfid ) {
      $user_id = get_current_user_id();
    	
      //if search form ID = 109, do something with this query
      if($sfid==109)
      {
        //modify $query_args here before returning it
        $query_args = array(
          'author' => $user_id
        );
      }	
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_user_posts', 20, 2 );
    

    Speaking of security issues: I will further investigate the URL-snooping issue. But I also have a content restrictions on single post view in other places, so posts can’t be viewed by non-authors anyway.

    Cheers,

    Georg

    Trevor Moderator
    #135095
    This reply has been marked as private.
    Georg
    #137453

    Hi Trevor,

    a quick update, just in case someone is looking for a solution. The following filter works for me,
    only posts written by currently logged-in user will be displayed.

    function filter_function_name( $query_args, $sfid ) {
      //if search form ID = 100, then do something with this query
      if($sfid==100) {
        //modify $query_args here before returning it
        $query_args = array(
          'post_type' => 'my_custom_post_type',
          'author' => $user->ID
        );
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Thanks,

    Georg

    Trevor Moderator
    #137490

    Thanks for sharing. I will close this thread for now.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Filter content by current user’ is closed to new replies.