Limiting search results to a specific Category

Forums › Forums › Search & Filter Pro › Limiting search results to a specific Category

Viewing 5 posts – 1 through 5 (of 5 total)
  • Anonymous
    #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
    #2886

    Hey there, just to let you know I will be looking at this tonight.

    Thanks

    Ross Moderator
    #3106

    Hey 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
    #3493

    In 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
    #3507

    Hey 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

Viewing 5 posts – 1 through 5 (of 5 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.