Multisite Tax – Edit tax query in pre_get_posts action
-
Basically I have several blogs and I have a taxonomy called mytax.
Each term of this taxonomy has a “custom” way to associate posts (not only from the same blog) to the term: saving term will add to every posts a new post_meta mytax_TERMBLOGID_TERMID.I would like to edit wp_query to retrieve from elasticsearch only the posts that have this post_meta. So I’m going to edit wp_query via the pre_get_posts action like this
add_action('pre_get_posts', function($query){ if (is_admin() || !$query->is_main_query()) { return; } if (!defined('EP_VERSION') || !is_tax('mytax')) { return; } $query->set('ep_integrate', true); $this_taxonomy = 'mytax'; $term_ID = get_current_blog_id() . '_' . $term->term_id; $meta_query = [ 'relation' => 'or', [ 'key' => "mytax_". $term_ID, 'value' => 1, 'operator' => '=', ] ]; $query->set('meta_query', $meta_query); $query->set('tax_query', false); },1,1);
Everything seems to work, except that the “tax_query” is not actually set to FALSE
term/queried_object dump
'term_id' => 1212 'name' => 'My term of tax mytax' 'slug' => 'my-term-of-tax-mytax' 'term_taxonomy_id' => 1231 'taxonomy' => 'mytax' 'blog_id' => 1
This query is made
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (1231) ) AND ( ( wp_postmeta.meta_key = 'mytax_1_1212' AND wp_postmeta.meta_value = '1' ) ) AND wp_posts.post_type IN ('post', 'video') AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 15
I do not need LEFT JOIN part, but I don’t understand how remove it.
If I do a simple wp_query in the tax page like this
$term = get_queried_object(); $term_ID = get_current_blog_id() . '_' . $term->term_id; $_wp_query = new WP_Query([ 'ep_integrate' => true, 'post_status' => 'any', 'post_type' => 'any', 'meta_query' => [ 'relation' => 'or', [ 'key' => "mytax_" . $term_ID, 'value' => 1, 'operator' => '=', ], ] ]);
it returns correctly posts from elastic search.
Is There a way to do that in main wp_query? Is there a way to edit args (a filter?) sended to elasticsearch?
Thank you
- The topic ‘Multisite Tax – Edit tax query in pre_get_posts action’ is closed to new replies.