M
M
Mkulya2021-07-26 15:50:24
WordPress
Mkulya, 2021-07-26 15:50:24

"pre_get_posts" how to get posts from the required category with taxonomy?

Hello!

Tell me how to get posts from a certain category "56k-modems", where the
"billion" tag is specified in the "brand" taxonomy.

The code I'm trying to change the selection of posts.

add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ){

  if($query->is_archive && isset($query->query["category_name"]) ){
     $taxquery = array(
      array(
        'taxonomy' => 'brand',
        'field' => 'slug',
        'terms' => 'billion',
        'operator'=> 'AND'
      )
    );
  
    $query->set( 'tax_query', $taxquery );
    $query->set('category_name', '56k-modems');
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mkulya, 2021-07-27
@Mkulya

Maybe someone will need it - the code is working, I just searched in a category where there was only one brand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question