I've used pre_user_query
to edit the actual sql query and it works wonderfully.
Now I'm trying to work with pre_get_posts
and its not at all the same thing. I need to do an INNER JOIN.. In this function it seems to only accept things like $query->set('meta_query',$meta_query);
which is too simple... The setting doesn't allow me to add a JOIN clause and then use it in a WHERE clause.. which is what I need to do.
Am I using the wrong function? Is there a pre_posts_query
that exists instead?? What are others doing here? Any guidance will be super appreciated!
I've used pre_user_query
to edit the actual sql query and it works wonderfully.
Now I'm trying to work with pre_get_posts
and its not at all the same thing. I need to do an INNER JOIN.. In this function it seems to only accept things like $query->set('meta_query',$meta_query);
which is too simple... The setting doesn't allow me to add a JOIN clause and then use it in a WHERE clause.. which is what I need to do.
Am I using the wrong function? Is there a pre_posts_query
that exists instead?? What are others doing here? Any guidance will be super appreciated!
1 Answer
Reset to default 0The pre_get_posts
function is a simple query modifier for general "post" queries. If you're looking to modify the SQL directly for these query I suggest using the post_clauses
hook. This hook passes an array of SQL clauses such as:
$sql['where']
and sql['join']
Here you can inject your own custom SQL into the specific clauses. That being said, if you are truly writing something custom it might be beneficial to circumvent WordPress entirely and just use $wpdb
to grab your results.
INNER JOIN
– Tom J Nowell ♦ Commented Aug 5, 2020 at 15:56pre_user_query
is not equivalent topre_get_posts
, there is apre_get_users
– Tom J Nowell ♦ Commented Aug 5, 2020 at 15:56