Using the pre_get_posts hook, I'm able to take $query as an argument. But on posts_where and posts_join hook, I receive the respective clauses and not the query. The problem is that the hook is running on every query, not just the main one.

Given the following code, the code will be executed once on each query the page runs:

add_filter('posts_join', 'my_join');
function my_join($join) { echo 'lala'; return $join; }

How can I tell if the query is the main one so it only runs once?

1 Answer 1


Of course! I just had to set the $accepted_args parameter of add_filter() to 2. Like so:

add_filter('posts_join', 'my_join', 10, 2);
function my_join($join, &$query) { echo $query->is_main_query() ? 'y' : 'n'; return $join; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.