在没有比较的情况下获得基于最新日期的帖子

时间:2017-03-10 13:32:35

标签: wordpress

我有自定义帖子类型,其元值包括上次更新日期。 我必须获得post id,其中meta值具有最新日期而不比较其他日期。 下面是我的wp查询来获取帖子ID,但它不起作用。

    $args = array(
    'posts_per_page' => 1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'post_status',
            'compare' => '=',
            'value' => 'active'
        ),
        array(
            'key' => 'other_cpt_id',
            'compare' => '=',
            'value' => $cptID
        ),
        array(
            'key' => 'post_last_update_date', 
            'value' => '', 
            'compare' => '>',
            'type' => 'DATE'
            )            
    ),
    'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
    'post_type' => 'custom_post_type',
    'post_status' => 'publish'
    );

1 个答案:

答案 0 :(得分:0)

如果您使用的是WP 4.2 + ,则可以尝试以下操作:

$args = array(
    'posts_per_page' => 1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'post_status',
            'compare' => '=',
            'value' => 'active'
        ),
        array(
            'key' => 'other_cpt_id',
            'compare' => '=',
            'value' => $cptID
        ),
        array(
            'key' => 'post_last_update_date', 
            'value' => '', 
            'compare' => '>',
            'type' => 'DATE'
            )            
    ),
    'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
    'post_type' => 'custom_post_type',
    'post_status' => 'publish'
);

有些消息来源:

General WP_Query:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

4.0+ orderby array:https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

4.2+ orderby元数据:https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/