Custom Taxonomy query to match all terms
-
Hello,
I am at my wit’s end trying to make a custom query work and just want to check and see whether this might have anything to do with the fact that the taxonomy was created with CPT UI and if there need to be any settings changed.
I need the query below to return posts which match ALL of the same terms (from the custom taxonomy ‘models’) as the current post. As it is, using the ‘AND’ operator returns no posts at all (there should be one on my test page). If I remove that it returns all posts that match ANY of the terms.
I saw another post that had the same problem with a different type of query and thought I’d check with support here to see if you have any ideas. Thanks!
add_action( 'elementor/query/model_loop_free', function( $query ) { //get ID of current post $id = get_queried_object_id(); // get current taxonomy - in this case the make/model $taxos = get_the_terms( $id, 'models'); foreach ( $taxos as $tax ) { $tax_slugs[] = $tax->slug; } $tax_query = array( 'relation' => 'AND', array( 'taxonomy' => 'models', 'field' => 'slug', 'terms' => $tax_slugs, 'operator' => 'AND' ), array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'free-play-adapter' ), ); $query->set( 'tax_query', $tax_query ); } );
The topic ‘Custom Taxonomy query to match all terms’ is closed to new replies.