• Hello, I’m looking to show a list of posts from a search in multiple categories. Complicated!!

    In general, my 'cat' => array(1,2,3) doesn’t work, but that’s another issue, though I think it’ll make an appearance in this thread.

    So, first, I need to define the content of the search – using the search_query?
    Then I need to direct the search to three different categories.

    I’d like to do this without setting new a new post_type, but if necessary, I did find this code that could help:

    function SearchFilter($query) {
      if ($query->is_search) {
        // Insert the specific post type you want to search
        $query->set('post_type', 'feeds');
      }
      return $query;
    }
    
    // This filter will jump into the loop and arrange our results before they're returned
    add_filter('pre_get_posts','SearchFilter');

    And, for reference if necessary, a good tutorial on setting up post_type.

    Thanks!

Viewing 1 replies (of 1 total)
  • Hi, there are some pages from the Codex which I’m sure can help you:

    Query_Overview
    Action_Reference/pre_get_posts
    Function_Reference/is_main_query
    Class_Reference/WP_Query
    WordPress_Query_Vars
    – also directly look in core file wp-includes/query.php

    there are different examples which you can adapt with appropriate conditional tags to fit your needs.

    in your case, taking the example from is_main_query and modifying a little: (didn’t test)

    function search_query_include_category( $query ) {
        if ( ! is_admin() && $query->is_search() && $query->get( 'cat' ) )
            $query->set( 'cat', '1,2,3' );
    }
    add_action( 'pre_get_posts', 'search_query_include_category' );

    and change conditions to include categories rather than exclude them, and if you need it, to restrict this kind of search only to happen within specific pages or areas.

    I spent some time reviewing and tryin’ out code examples found in the links I gave you above, and those are, for me, reference pages priceless utility when customizing functions and behaviours in a theme or a child-theme.

    hope those may help you too, cheers!

Viewing 1 replies (of 1 total)
  • The topic ‘Search posts in a specific category’ is closed to new replies.