简体   繁体   English

MySQL / WordPress元查询; 结合多个条件

[英]MySQL / WordPress meta query; combining multiple conditions

Apologies if this is fairly basic, I'm a little out of my depth and could use a bit of guidance to fill in some blanks. 抱歉,如果这是很基本的话,我有点儿不了解了,可以使用一些指导来填补一些空白。

Here's the situation... I'm querying the WordPress database to return a subset of posts which match a set of specific criteria relating to meta key/value conditions (specifically, whether a key of 'dates' contains a value which matches the next X days). 这是一种情况...我正在查询WordPress数据库,以返回帖子的子集,该帖子的子集与与元键/值条件有关的一组特定条件匹配(具体来说,“日期”键是否包含与下一个匹配的值) X天)。 This works fine - the code is currently: 效果很好-该代码当前为:

$request  = "SELECT DISTINCT ID FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='days'";
$request .= " AND $wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value >= '$startDate' AND $wpdb->postmeta.meta_value <= '$endDate' AND $wpdb->postmeta.meta_value != '$today'";

Now, I'd like to introduce another scenario, and also return any posts which have a key of *_dayType*, and a value of month , where month is the current month. 现在,我想介绍另一种情况下, 返回其拥有的* _dayType *的关键,和的值,其中一个月是当月任何职位。 I've extended the code to allow for an OR rule to also return posts which match the first day of the month. 我扩展了代码,以允许OR规则也返回与当月第一天匹配的帖子。 The extended code looks like: 扩展代码如下:

$request  = "SELECT DISTINCT ID FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='days'";
$request .= " AND ";
$request .= " (($wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value >= '$startDate' AND $wpdb->postmeta.meta_value <= '$endDate' AND $wpdb->postmeta.meta_value != '$today')";
$request .= " OR ";
$request .= " ($wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value = '$firstDayOfMonth' ))";

Now I run into a problem. 现在我遇到了一个问题。 I want to expand the conditions for returning the new month posts by querying the meta_key variable again. 我想通过再次查询meta_key变量来扩展返回新月份帖子的条件。 Eg, 例如,

$wpdb->postmeta.meta_key = '_dayType' AND $wpdb->postmeta.meta_value = 'month'

However, adding this in returns zero results. 但是,将其添加将返回零结果。 I've tested querying just one of the conditions (either matching the first day of the month, or matching being a month ), and I can return one or the other with no problem. 我已经测试了查询条件中的一个 (匹配月份的第一天,还是匹配一个月 ),并且可以毫无问题地返回一个或另一个。 I'm pretty sure that the problem is that I'm querying the same variable twice in the same string, with no consideration for ordering or distinction. 我很确定问题是我在同一字符串中两次查询同一变量,而没有考虑排序或区分。 What's the best way of reconstructing this query to bring back: 重构此查询以带回的最佳方法是什么:

  • Posts which match the next X days OR 与接下来的X天匹配的帖子,或者
  • Posts which are 'months' “月”的帖子

Here's the final mark-up. 这是最后的加价。 Is the problem just my AND nesting syntax? 问题仅仅是我的AND嵌套语法吗?

$request  = "SELECT DISTINCT ID FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='days'";
$request .= " AND ";
$request .= " (($wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value >= '$startDate' AND $wpdb->postmeta.meta_value <= '$endDate' AND $wpdb->postmeta.meta_value != '$today')";
$request .= " OR ";
$request .= " (($wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value = '$firstDayOfMonth') AND ($wpdb->postmeta.meta_key = '_daytype' AND $wpdb->postmeta.meta_value = 'month')) )";

Resolved; 解决; needed to query multiple keys via OR, rather than AND, as the variable couldn't have two values at once now, could it? 需要通过OR而不是AND来查询多个键,因为该变量现在不能一次具有两个值,对吗?

SELECT DISTINCT ID FROM wp_posts, wp_postmeta 
WHERE (
 wp_posts.ID = wp_postmeta.post_id
 AND post_status = 'publish'
 AND post_type = 'days'
 AND (
  wp_postmeta.meta_key = 'dates' 
  AND wp_postmeta.meta_value >= '$startDate' 
  AND wp_postmeta.meta_value <= '$endDate' 
  AND wp_postmeta.meta_value != '$today'
 )
)
UNION
SELECT DISTINCT ID FROM wp_posts, wp_postmeta 
WHERE (
 wp_posts.ID = wp_postmeta.post_id
 AND post_status = 'publish'
 AND post_type = 'days'
 AND (
 (wp_postmeta.meta_key = '_dayType' AND wp_postmeta.meta_key = 'month')
 OR
 (wp_postmeta.meta_key = 'dates' AND wp_postmeta.meta_value = '$firstDayOfMonth' )
 )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM