admin管理员组

文章数量:1327278

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!

Share Improve this question asked Aug 5, 2020 at 15:31 user1806791user1806791 1113 bronze badges 2
  • What are you trying to do? It might be more useful to ask about that, there's likely a better option that avoids needing an INNER JOIN – Tom J Nowell Commented Aug 5, 2020 at 15:56
  • Also pre_user_query is not equivalent to pre_get_posts, there is a pre_get_users – Tom J Nowell Commented Aug 5, 2020 at 15:56
Add a comment  | 

1 Answer 1

Reset to default 0

The 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.

本文标签: wp querypreuserquery vs pregetposts