I have complicated query where I need to join multiple tables. I’m doing that via add_filter('posts_join')
– my question is, when and where do I add the filter so that it only applies to the custom query?
My Custom Query
$test = new WP_Query(array('post_type' => 'artcpt', 'posts_per_page' => 10, 'tax_query' => array(array('taxonomy' => 'arttax', 'field' => 'id', 'terms' => array(20,41,16)))));
Should I physically add the filter above the query or after? Do I need to put it into a separate file (function.php) and attach it another way? Do I need to do something with query_vars
?
You want to add your filter before you create the query:
where you can define the filter callback in your theme files, for example
functions.php
or in a plugin:Notice that we remove the filter with this line:
so it will not affect other queries.