Forums Forums Search & Filter Pro filter authors on results

Viewing 10 posts - 1 through 10 (of 10 total)
  • Anonymous
    #73049

    Hi,
    I’m trying to hide some authors by id from the main wp query and the search queries.
    Does the “pre_get_post filter” work on your search result query?
    Do I need to specify the form id?

    This is the code I’m trying to use but it doesn’t work:

    //hide posts from search results and wp_query
    
    function filter_hide( $query_args ) {
    
    	$meta = get_user_meta(get_current_user_id(), 'hide', true);
    	
    	//if search form ID = 2259, then hide authors' posts
    	
    	if(!is_admin() )
    	{
    		$args = array(
        			'author__not_in' => $meta,
    		);
    	}
    	
    	return $query_args;
    	
    }
    add_action( 'pre_get_posts', 'filter_hide', 10, 2 );
    
    Anonymous
    #73052

    Ok, I tryed the code you suggest in your documentation also, and doesn’t work:

    //hide posts from search results
    
    function filter_hide( $query_args, $sfid ) {
    
    	$meta = get_user_meta(get_current_user_id(), 'hide', true);
    	
    	//if search form ID = 2259, then hide authors' posts
    	
    	if($sfid==2259)
    	{
    		$args = array(
        			'author__not_in' => $meta,
    		);
    	}
    	
    	return $query_args;
    
    }
    add_action( 'pre_get_posts', 'filter_hide', 10, 2 );
    Trevor
    #73101

    Can you show me a link to where you got that code from Laura?

    Anonymous
    #73107

    here is the link from your documentation:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    Here is a sample of pre_get_posts() usage to add arguments to the search query:

    https://support.searchandfilter.com/forums/topic/post-types-in-alphabetical-order/

    Thanks Trevor

    Trevor
    #73109

    I will need to show this to the developer to get his input.

    Ross Moderator
    #73269

    Hi Laura

    I see potentially a few areas where something may be going wrong.

    1) pre_get_posts is an action, however our sf_edit_query_args is a filter, and you are trying to combine the two

    2) Our docs mention you should not use pre_get_posts and use our own filter instead.. Therefor, you must use our filter as per the docs:
    add_filter( 'sf_edit_query_args', 'filter_hide', 20, 2 );

    3) Your second code snippet should be close to working once you get it setup with our filter rather than pre_get_posts, however, I see another line where I’m not sure the logic is correct, I see you are trying to set author__not_in.. I’ve not used this before but assume it for excluding authors? If its like post__not_in then it will take an array of IDs..

    I see you are using $meta = get_user_meta(get_current_user_id(), 'hide', true); to get teh values for this field – what does this get? It doesn’t look like it gets the Author IDs that you want to exclude from your search? Why not try as a test excluding some author IDs that are known to you, so this should really be:
    $meta = array(1, 22);
    If the authors you wanted to exclude had the IDs of 1 and 22

    4) Then finally, you are setting your exclude authors into the variable $args but then you return a different variable, $query_args, so your changes are not being used at all or passed back to S&F…

    I’ve rewritten your code (untested) in to how it should look:

    function filter_hide( $query_args, $sfid ) {
    
    	//$meta = get_user_meta(get_current_user_id(), 'hide', true);
    	
    	//if search form ID = 2259, then hide authors' posts
    	
    	if($sfid==2259)
    	{
    		//these are the Author IDs you want to exclude
    		$query_args['author__not_in'] = array(1, 10); 
    	}
    	
    	return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', 'filter_hide', 50, 2 );

    If you wanted to hide only the current users posts you would change 1 line above to something like:

    $query_args['author__not_in'] = array(get_current_user_id());

    Hope that helps

    Anonymous
    #73421

    I’m testing your code Ross, and appreciate your help a lot.
    Thanks for your time, I’ll let you know if it worked.

    Anonymous
    #74338

    Thanks ross,
    the code works fine thanks to the variable name change (my mistake), the meta data was an array (already tested) so the point was not the problem.
    Hope this ticket will help you developing your new user meta feature about which Trevor told me in another thread.
    Thanks a lot for your help.
    Regards,
    LauraC

    Trevor
    #74340

    Can I close this now Laura?

    Anonymous
    #74342

    Sure, thanks and sorry for the late answer…

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