Forums › Forums › Search & Filter Pro › Limiting search results to a specific Category
- This topic has 4 replies, 3 voices, and was last updated 10 years, 3 months ago by Ross.
-
Anonymous(Private) July 23, 2014 at 2:24 pm #2854
Hi,
I found this article which is exactly what I need – http://wordpress.org/support/topic/limiting-search-results-to-a-specific-category
I noticed that you said drop a line in the pro support forum to get the hook code so is there any chance I can get that code?
Thanks for your help in advance.
Ross Moderator(Private) July 24, 2014 at 1:42 pm #2886Hey there, just to let you know I will be looking at this tonight.
Thanks
Ross Moderator(Private) August 1, 2014 at 1:05 am #3106Hey Wayne
Sorry for the late reply – there have been so many support requests recently its easy to miss one – to limit your categories add this to your functions.php :
function filter_results( $query ) { global $sf_form_data; global $wp_query; if ( $sf_form_data->is_valid_form() && $query->is_main_query() && !is_admin() ) { $query->set('cat', '-1,-2,-3'); //you can use any query modifications from here - http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts } } add_action( 'pre_get_posts', 'filter_results', 21 );
Hopefully this works for you 🙂
Anonymous(Private) August 14, 2014 at 3:48 am #3493In the aforementioned post, you mention adding that code to “functions.php”. I am not finding that file in the search-filter-pro plugin directory, and I am a bit hesitant to add it anywhere else. Can you clarify where to put it?
In general my use case is that I have two pages: one for movie reviews and one for book reviews. On the movie page, I want to search across movie posts. On the book page, I want to search across book posts. I think I can do this by simply limiting the category used in the search to “movie” or “book” — depending on which page the search bar is used. (This might be possible by using the “exclude_ids”, but I would rather include one specific category — a white list instead of a black list.)
I think I’m close to being able to get Search and Filter Pro to be able to do this, but I still can’t get the category aspects functioning correctly. I’d love to apply a search filter that would automatically search on only the category of “movie” or “book” respectively.
Ross Moderator(Private) August 14, 2014 at 8:29 pm #3507Hey Matthew
functions.php is the file you will find in the root of your theme directory – this is a common place to add custom code – let me know if you need further info (http://codex.wordpress.org/Functions_File_Explained)
In 2 weeks I will be able to work full time on Search & Filter again, and at that time you can expect a lot of updates and improvements, and setting up default categories etc is a common request and high on my to do list – so expect this to be integrated in the plugin in the near future!
Thanks
-
AuthorPosts