且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Wordpress - 多个 WP Query 对象合二为一?

更新时间:2023-11-30 23:28:46

您想要的将转换为 WHERE ... OR ... 条件或 UNION在 SQL 中,例如.

what you want would translate to a WHERE ... OR ... condition or a UNION in SQL, eg.

SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page') 
  OR (cat = 1 AND post_type = 'post')

SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
  UNION
SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'

从查看源和 WP 从 WP_Query() 构造 SQL 的方式,我认为这是不可能的:没有查询变量的 OR'ing 或 UNION.

from looking at the source and the way WP constructs SQL from WP_Query(), i don't think this is possible: there is no OR'ing nor UNION of query vars.

我唯一想到的是编写一个实现 posts_where 的插件 过滤器(应用于返回 post 数组的查询的 WHERE 子句).您可以使用不同的 WP 查询调用此插件,该插件将获取它们的 WHERE 部分,并且可以将它们 OR 放在一起.

the only thing that comes to my mind is writing a plugin that implements the posts_where filter (applied to the WHERE clause of the query that returns the post array). you could call this plugin with your different WP Querys, and the plugin would get their WHERE parts and could OR them together.

另见http://codex.wordpress.org/Custom_Queries