自定义字段排序不起作用(WP 3.8.1)

时间:2014-03-26 作者:Panagiotis

我正在尝试使用日期格式YYYYMMDD(php:date(\'Ymd\')对自定义wp\\u查询进行排序。这不会被视为日期字段,而是存储在自定义字段中的数字字段。

我的WP-QUERY代码如下:

$today = date("Ymd", strtotime(current_time(\'mysql\')));

$args = array(
 "post_type" => "post",
 "post_status" => "publish",
 "posts_per_page" => -1,
 "cat" => 13,
 "meta_query" => array(
  array(
   "key" => "event-date",
   \'type\' => \'NUMERIC\',
   \'value\' => $today,
   "compare" => ">="
  )
 ),
 \'orderby\' => \'meta_value_num\',                            
 \'order\' => \'ASC\'
 );
 $eventsbody = new WP_Query($args);  
 var_dump($eventsbody->request);
转储时,它会生成以下代码:

"SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) LEFT JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type LIKE \'post\\_%\' LEFT JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (13) ) AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\') AND (wp_posts.post_status = \'publish\') AND ( (wp_postmeta.meta_key = \'event-date\' AND CAST(wp_postmeta.meta_value AS DATE) >= \'20140326\') ) AND (t.language_code=\'el\' OR t.language_code IS NULL ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC " 
问题是,它忽略了meta\\u value\\u num排序,而是按wp\\u posts进行排序。post\\u日期。是wp3吗。8.1问题?

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

我认为您的查询参数缺失meta_key, 在这种情况下\'event-date\'. 您需要在元查询之外使用它,orderby才能正常工作。尝试以下操作:

$today = date("Ymd", strtotime(current_time(\'mysql\')));

$args = array(
  "post_type" => "post",
  "post_status" => "publish",
  "posts_per_page" => -1,
  "cat" => 13,
  "meta_query" => array(
   array(
    "key" => "event-date",
    \'type\' => \'NUMERIC\',
    \'value\' => $today,
    "compare" => ">="
  )
  ),
  \'meta_key\' => \'event-date\', // meta_key added
  \'orderby\' => \'meta_value_num\',                            
  \'order\' => \'ASC\'
  );
  $eventsbody = new WP_Query($args); 

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post