Modify query after meta value

时间:2013-03-19 作者:Gigi Becali

我想更改日历,以便根据自定义日期字段显示一些帖子。目前,我有兴趣修改此查询:

$wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    ."FROM $wpdb->posts "
    ."WHERE post_date >= \'{$thisyear}-{$thismonth}-01 00:00:00\' "
    ."AND post_date <= \'{$thisyear}-{$thismonth}-{$last_day} 23:59:59\' "
    ."AND post_type = \'post\' AND post_status = \'publish\'"
);
对这样的事情:

$wpdb->get_results("SELECT a.ID, a.post_title, DAYOFMONTH(b.meta_value) as dom "
    ."FROM $wpdb->posts a, $wpdb->postmeta b "
    ."WHERE b.meta_value >= \'{$thisyear}-{$thismonth}-01 00:00:00\' "
    ."AND b.meta_value <= \'{$thisyear}-{$thismonth}-{$last_day} 23:59:59\' "
    ."AND a.post_type = \'post\' AND a.post_status = \'publish\' AND a.ID = b.postid AND b.meta_key=\'Event Date\'"
);
我确实尝试了上面的查询,但它不起作用,它返回一个空数组。如何更改日历将关注元值而不是post\\u日期?非常感谢。

1 个回复
SO网友:Milo

下面是一个使用WP_Query\'smeta_query 要在两个元键值之间查询帖子,请执行以下操作:

$meta_query = array(
    array(
        \'key\' => \'event_date\',
        \'value\' => array( $date_start, $date_end ),
        \'compare\' => \'BETWEEN\'
    )
);

$args = array(
    \'posts_per_page\' => -1,
    \'meta_key\' => \'event_date\',
    \'orderby\' => \'meta_value\',
    \'meta_query\' => $meta_query
);

$results = new WP_Query( $args );

结束