尝试在ORDER BY设置为字段值的情况下执行复杂的自定义字段查询

时间:2012-08-06 作者:DaveE

我有两个活动帖子的自定义字段:event-startevent-end. 当我查询当前事件时,我想显示event-start 或者event-end 大于或等于今天的日期,我想按event-start (不是默认的post\\u日期)。

我使用以下参数创建了一个查询:

$args = array(
  \'post_type\' => \'post\',
  \'posts_per_page\' => $number,          
  \'meta_query\' => array(
    \'relation\' => \'OR\',
    array(
      \'key\' => \'event-start\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    ),
    array(
      \'key\' => \'event-end\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    )
  ),
  \'orderby\' => \'meta_value\',
  \'order\' => \'ASC\',
);
查询得到了正确的帖子,但问题是我无法让orderby正常工作。这组参数运行以下查询:

SELECT SQL_CALC_FOUND_ROWS wp_18_posts.ID FROM wp_18_posts INNER JOIN wp_18_postmeta ON (wp_18_posts.ID = wp_18_postmeta.post_id)
INNER JOIN wp_18_postmeta AS mt1 ON (wp_18_posts.ID = mt1.post_id) WHERE 1=1 AND wp_18_posts.post_type = \'post\' AND (wp_18_posts.post_status = \'publish\' OR wp_18_posts.post_status = \'private\') AND ( (wp_18_postmeta.meta_key = \'event-start\' AND CAST(wp_18_postmeta.meta_value AS SIGNED) >= \'1344289896\')
OR (mt1.meta_key = \'event-end\' AND CAST(mt1.meta_value AS SIGNED) >= \'1344289896\') ) GROUP BY wp_18_posts.ID **ORDER BY wp_18_posts.post_date** ASC LIMIT 0, 3
请注意,ORDER BY子句仍设置为post\\U date。如果我将查询参数更改为使用旧样式的meta\\u键以及orderby meta\\u值以及新样式的meta\\u查询,如下所示:

$args = array(
  \'post_type\' => \'post\',
  \'posts_per_page\' => $number,      
  \'meta_query\' => array(
    \'relation\' => \'OR\',
    array(
      \'key\' => \'event-start\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    ),
    array(
      \'key\' => \'event-end\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    )
  ),
  \'orderby\' => \'meta_value\',
  \'meta_key\' => \'event-start\',
  \'order\' => \'ASC\',
);
我最终得到以下查询:

SELECT SQL_CALC_FOUND_ROWS wp_18_posts.ID FROM wp_18_posts INNER JOIN wp_18_postmeta ON (wp_18_posts.ID = wp_18_postmeta.post_id)
INNER JOIN wp_18_postmeta AS mt1 ON (wp_18_posts.ID = mt1.post_id)
INNER JOIN wp_18_postmeta AS mt2 ON (wp_18_posts.ID = mt2.post_id) WHERE 1=1 AND wp_18_posts.post_type = \'post\' AND (wp_18_posts.post_status = \'publish\' OR wp_18_posts.post_status = \'private\') AND (wp_18_postmeta.meta_key = \'event-start\'
OR (mt1.meta_key = \'event-start\' AND CAST(mt1.meta_value AS SIGNED) >= \'1344289518\')
OR (mt2.meta_key = \'event-end\' AND CAST(mt2.meta_value AS SIGNED) >= \'1344289518\') ) GROUP BY wp_18_posts.ID ORDER BY wp_18_postmeta.meta_value ASC LIMIT 0, 3
用一个event-start 元键,而不考虑为该键设置的日期。

如何通过event-start 如有价值,将不胜感激。

2 个回复
最合适的回答,由SO网友:DaveE 整理而成

以下是我找到的最佳解决方案:

$args = array(
    \'post_type\'      => \'post\',
    \'posts_per_page\' => $number,
    \'meta_key\'       => \'event-start\',
    \'meta_value\'     => $today_is,
    \'meta_query\'     => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'event-start\',
            \'value\'   => $today_is,
            \'type\'    => \'NUMERIC\',
            \'compare\' => \'>=\',
        ),
        array(
            \'key\'     => \'event-end\',
            \'value\'   => $today_is,
            \'type\'    => \'NUMERIC\',
            \'compare\' => \'>=\',
        )
    ),
    \'orderby\' => \'meta_value\',
    \'order\'   => \'ASC\',
);
它使用相同的复合体meta_query, 但我添加了一个单独的meta_keymeta_value 为了触发meta_value 订货人。

如果我没记错的话,在我写这篇文章的时候,我曾尝试过这个解决方案,但没有成功。我还相信,我在WordPress Core中遇到了一张与此问题相关的Trac票证,但我一生都找不到它。

一句话:在最初发布这篇文章时,我认为有一个WordPress bug阻止了它的工作,但现在它可以工作了。

SO网友:unifiedac

如果自定义字段的值是数字,可以尝试使用meta\\u value\\u num排序。还请注意,meta\\u value和meta\\u value\\u num都需要查询中的meta\\u键,如下所述here.

$args = array(
  \'post_type\' => \'post\',
  \'posts_per_page\' => $number,          
  \'meta_query\' => array(
    \'relation\' => \'OR\',
    array(
      \'meta_key\' => \'event-start\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    ),
    array(
      \'meta_key\' => \'event-end\',
      \'value\' => $today_is,
      \'type\' => \'NUMERIC\',
      \'compare\' => \'>=\'
    )
  ),
  \'orderby\' => \'meta_value_num\',
  \'order\' => \'ASC\',
);

结束

相关推荐

Order posts by date

我是一个女孩,请原谅我的英语。也许这很容易,但我却不知道这一点。但问题是我想在主页上输出最近3天的帖子。像这样:30/03/12 职位1职位2岗位329/03/12 职位1职位2岗位328/03/12 职位1职位2岗位3我试过:$loopday = \'\'; if ( have_posts() ) : while ( have_posts() ) : the_post(); if ($loopday !== get_the_time(\'D M j\')