如何使用wp_query订购帖子列表?

时间:2018-07-10 15:52:43

标签: wordpress

我想用wp_query订购我的列表。我有带有meta_key'featured_post'的帖子,并且对于不同的帖子,该键的三个值分别是:'1','2'和三次键不存在。

我现在正在使用此wp_query结构:

global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
    $wp_query->set('paged', $args['paged']);
    $paged = $args['paged'];
}
$temp = $wp_query;
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 20,
    'paged' => $paged,
    'meta_query' => array(
        'relation' => 'OR',
        'featured_clause_first' => array(
            'key' => 'featured_post',
            'value' => '1',
            'compare' => '='
        ),
        'featured_clause_second' => array(
            'key' => 'featured_post',
            'value' => '2',
            'compare' => '='
        ),
        'featured_clause_third' => array(
            'key' => 'featured_post',
            'compare' => 'NOT EXISTS',
            'value' => '1'
        )
    ),
    'orderby' => array( 
        'featured_clause_first' => 'DESC',
        'featured_clause_second' => 'DESC',
        'featured_clause_third' => 'DESC'
    ),

);
$wp_query = null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
    get_template_part( 'templates/single');
endwhile;
wp_reset_postdata();
wp_reset_query();

但是我的命令有误。最后,我需要看到顶部的值为“ 1”的帖子以及其他值为“ 2”且之后不存在键的帖子。

注意:我不能使用“'meta_key'=>'featured_post'”,因为这样就不会显示没有现有meta_key的帖子。

1 个答案:

答案 0 :(得分:0)

好的。如果未选中功能,则我已编辑了保存metabox函数以删除metakey。这样我只有两个值:1和键不存在。之后,我需要使用此meta args命令:

$sort_meta_query = array(
    'relation' => 'OR',
    'featured_clause' => array(
        'key' => 'featured_post',
        'value' => '1',
        'compare' => '='
    ),
    array(
        'key' => 'featured_post',
        'compare' => 'NOT EXISTS',
        'value' => '1'
    )
);
$sort = array(
    'orderby' => array(
        'featured_clause' => 'DESC',
        'date' => 'DESC'
    )
);