自定义查询:多种自定义帖子类型

发布于 2024-11-05 18:00:59 字数 925 浏览 0 评论 0原文

我在从 自定义 WordPress 查询 中提取多个自定义帖子类型时遇到一些问题。我创建自定义 WordPress 查询而不是使用 query_postsWP_query 的原因是因为我根据投票插件添加的信息对帖子进行排序,并且必须加入该查询插件的表,因此内置查询不是一个选项。

我的问题是如何在同一查询中包含多个自定义帖子类型?目前,我的查询如下所示:

$query = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID         = wpostmeta.post_id
    AND wposts.post_status  = 'publish'
    AND wposts.post_type    = 'TWO_POST_TYPES'
    AND post_date       >= '$startdate'
    AND post_date       <= '$enddate'
    GROUP BY wposts.ID
    ";

我试图将两种不同的自定义帖子类型放入 wposts.post_type 部分,我们可以将其称为 type1类型2。我已经尝试过以下内容,但没有成功:

  1. array('type1', 'type2')
  2. 'type1, type2'

我也尝试过传递这两个作为查询中的变量,但也没有运气。有人可以帮我吗?

I am having some trouble pulling multiple custom post types from a Custom Wordpress Query. The reason I am creating a custom Wordpress query rather than using query_posts or WP_query is because I am sorting my posts based on information added by a voting plugin, and have to join that plugin's table, so the built-in queries are not an option.

My question is how can I include multiple custom post types in the same query? At present, my query looks like the following:

$query = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID         = wpostmeta.post_id
    AND wposts.post_status  = 'publish'
    AND wposts.post_type    = 'TWO_POST_TYPES'
    AND post_date       >= '$startdate'
    AND post_date       <= '$enddate'
    GROUP BY wposts.ID
    ";

I am trying to put two different custom post types into the wposts.post_type part, which we can call type1 and type2. What I have already tried is the following, with no luck:

  1. array('type1', 'type2')
  2. 'type1, type2'

I have also tried passing both of these as variables in the query, but also no luck. Would anybody be able to give me a hand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-11-12 18:00:59

你是这个意思吗?

AND wposts.post_type IN ('type1', 'type2')

这实际上意味着:

AND ( wposts.post_type = 'type1'
   OR wposts.post_type = 'type2'
    )

Do you mean this?

AND wposts.post_type IN ('type1', 'type2')

which actually means:

AND ( wposts.post_type = 'type1'
   OR wposts.post_type = 'type2'
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文