最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - pre_user_query vs pre_get_posts

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论