admin管理员组

文章数量:1123797

I want that my search page give results of posts (that i want that show) and taxonomy

The code to get just two type of posts:

function searchfilter($query) {

      if ($query->is_search && !is_admin() ) {
          $query->set('post_type',array('lesson','series'));
      }
 return $query;
}

add_filter('pre_get_posts','searchfilter');

I want it to look for taxonomy too Thanks

I want that my search page give results of posts (that i want that show) and taxonomy

The code to get just two type of posts:

function searchfilter($query) {

      if ($query->is_search && !is_admin() ) {
          $query->set('post_type',array('lesson','series'));
      }
 return $query;
}

add_filter('pre_get_posts','searchfilter');

I want it to look for taxonomy too Thanks

Share Improve this question edited Dec 18, 2017 at 21:05 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Dec 18, 2017 at 17:52 user133503user133503 312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can add a custom tax_query to your filter, and pass the search query to it:

function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $s = $query->get( 's' );
        $query->set('post_type', [ 'lesson', 'series' ] );
        $query->set( 
            'tax_query', 
            [
                [
                    'taxonomy' => 'your-taxonomy', 
                    'field'    => 'name', 
                    'terms'    => $s ,
                ] 
            ] 
        );
    }
    return $query;
}

add_action('pre_get_posts','searchfilter');

本文标签: filtersSearch by type posts and taxonomy