Wordpress - 多个 WP Query 对象合二为一? [英] Wordpress - multiple WP Query objects into one?

查看:56
本文介绍了Wordpress - 多个 WP Query 对象合二为一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Wordpress 中,可以为循环创建自己的 WP 查询.一个例子是这样的:

In Wordpress it's possible to create own WP Querys for the loop. An example is this:

$my_query = new WP_Query(array('post_parent' => 3, 'post_type' => 'page'));

另一个例子是:

$my_query = new WP_Query(array('cat' => 1, 'post_type' => 'post'));

我想要一个循环显示页面和来自同一循环的帖子.

I want a loop that presents pages AND posts from the same loop.

现在我的问题.是否可以将这两个对象合二为一?如果是,如何?我对创建两个不同的循环不感兴趣.

Now to my question. Is it possible to combine these two objects into one? If it is, how? I'm NOT interested in creating two different loops.

推荐答案

您想要的将转换为 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

这篇关于Wordpress - 多个 WP Query 对象合二为一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆