我有自定义帖子类型事件,其中包含自定义字段event_date。此字段的日期格式设置为Ymd。
我正在尝试按event_date
的顺序发帖子以下是按顺序提取帖子的代码
$today = date('Ymd');
$args = array (
'post_type' => array( 'event' ),
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' =>'meta_value', // also tried having meta_value_num but that didn't worked as well
'meta_key' =>'event_date',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
)
),
'suppress_filters' => true
);
/*
global $wp_filter;
var_dump( $wp_filter['posts_orderby'] );die;
This returns NULL as well
*/
$query = new WP_Query( $args );
echo '<pre>';
var_dump($query->request);die;
但$ query-&gt;请求将ORDER BY显示为post_date rahter而不是我提供的查询。 这是请求转储
string(681) "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND (
wp_posts.ID NOT IN (
SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (11)
)
) AND (
wp_postmeta.meta_key = 'event_date'
AND
(
( mt1.meta_key = 'event_date' AND mt1.meta_value >= '20171113' )
)
) AND wp_posts.post_type = 'event' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC "
无法通过event_date排序。 我究竟做错了什么?? 甚至尝试过
remove_all_filters('posts_orderby');
之前wp_query仍然是相同的结果。
最后:感谢大家的回复,最后通过在我的查询中将order_by更改为orderby来解决问题。 谢谢@Prasanna Venkatesh允许我签入phpmyadmin。在phpmyadmin中运行代码并修复它。 PS:除了order_by之外,sql查询是正确的。
答案 0 :(得分:0)
您错过了'type' => 'DATE'
元查询。
$args = array (
'post_type' => array( 'event' ),
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' =>'meta_value', // also tried having meta_value_num but that didn't worked as well
'meta_key' =>'event_date',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
)
),
'suppress_filters' => true
);
答案 1 :(得分:0)
使用&#39; meta_value_num&#39;在你的orderby类型和
$args = array (
'post_type' => array( 'event' ),
'posts_per_page' => -1,
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC'
)
),
'suppress_filters' => true
);