我正在尝试使用日期格式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问题?
最合适的回答,由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);