Forums › Forums › Search & Filter Pro › filter authors on results
Tagged: filter, Hide, pre get posts, search results
- This topic has 9 replies, 3 voices, and was last updated 7 years, 11 months ago by Anonymous.
-
Anonymous(Private) November 30, 2016 at 9:26 am #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(Private) November 30, 2016 at 9:41 am #73052Ok, 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 );
Anonymous(Private) November 30, 2016 at 11:01 am #73107here is the link from your documentation:
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
Ross Moderator(Private) November 30, 2016 at 4:37 pm #73269Hi Laura
I see potentially a few areas where something may be going wrong.
1)
pre_get_posts
is an action, however oursf_edit_query_args
is a filter, and you are trying to combine the two2) 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 likepost__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 of1
and22
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(Private) December 6, 2016 at 2:38 pm #74338Thanks 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 -
AuthorPosts