【问题标题】:Prioritizing wp_query by meta key按元键优先级 wp_query
【发布时间】:2018-12-19 06:53:43
【问题描述】:

我有两个自定义视图字段。 weekly_viewsall_views。每周视图自定义字段每周都会被删除,并从 0 开始再次计算视图。所以现在我想要实现的是按每周视图显示 12 个帖子,但是当自定义字段被删除时,除非这些帖子有视图,否则查询不会显示任何内容.我想在这里显示all_views 的帖子,而不是没有帖子。

我的查询如下,但它没有按我的意愿工作。简而言之,我想要实现的是通过weekly_views 自定义字段显示帖子,但如果没有帖子,则通过all_views 显示帖子。此外,如果 weekly_views 的帖子少于 12 个,则先显示 weekly_views 的帖子,然后再显示 all_views 的剩余帖子。

$args = array(
    'post_type'  => array( 'custom_post_type_1', 'custom_post_type_2'),
    'posts_per_page' => '12',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',                  
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'weekly_views',    
        ),
        array(
            'key'     => 'all_views',
        ),
    ),
);

上面的代码返回了我的帖子,但按 all_views 排序。


编辑

对我有用的新查询

<?php
$args = array(
    'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
    'posts_per_page' => '12',
    'meta_key' => 'weekly_views',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    );
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
    'post_type'=> array( 'band', 'artist'),
    'posts_per_page' => $showallpostscount,
    'meta_key' => 'all_views',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    );

$the_query2 = new WP_Query( $args2 );
}

?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

//Code to show posts goes here

<?php
endwhile;
wp_reset_postdata();
?>

<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>

//Code to show posts goes here

<?php
endwhile;
wp_reset_postdata();
?>

【问题讨论】:

    标签: wordpress custom-fields


    【解决方案1】:

    如果你想要更少的代码,你也可以这样做

    <?php
        $args = array(
            'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
            'posts_per_page' => '12',
            'meta_key' => 'weekly_views',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
        );
        $args2 = array(
            'post_type'=> array( 'band', 'artist'),
            'posts_per_page' => '12',
            'meta_key' => 'all_views',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
        );
        if ($query->post_count > 12) {
            $query_args = $args;
        }else if($query->post_count < 12){
            $query_args = $args2;
        }
    
        $query = new WP_Query( $query_args );
    
        while ($query -> have_posts()) : $query -> the_post(); 
        //Code to show posts goes here
    
        endwhile;
        wp_reset_postdata();
    ?>
    

    【讨论】:

    • 您好,添加'meta_key' =&gt; 'weekly_views',仅显示weekly_views 的帖子。但是,如果weekly_views 没有帖子,并且正如我所说,这个自定义字段每周都会被删除,那么它不会返回 all_views 的帖子。我想要实现的是按weekly_views 显示帖子,但如果没有帖子,则按all_views 显示帖子。此外,如果weekly_views 的帖子少于12 个,则首先显示weekly_views 帖子,然后显示all_views 的剩余帖子。我不需要代码,我只需要知道我哪里出错了。谢谢。
    • 您好,感谢您的编辑。我实际上已经从您所做的编辑中弄清楚了。我已经在上面的问题中添加了我现在使用的新参数,如果你认为我可以更好地改进这一点,请告诉我。
    • 嘿,我有一个脚本,我在不久前写了一个脚本,如果你愿意,我可以与你分享。也许它会让你更好地控制你如何显示你的帖子。
    • 我将这些帖子显示为滑块,因此不需要分页。非常感谢您的编辑,我会对此进行测试。
    • 为什么我的回答被接受了,没有其他人回答,但我的问题被标记了......我看不出我的帖子有什么问题......
    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多