doubi4435 2016-06-07 03:00
浏览 50
已采纳

Wordpress - 循环在ID号数组中找到的帖子

I have a list of post id's which I would like to loop the posts for, I have tried using the WP_QUERY with post__in, but this does not keep the order that the id's are listed in inside the $sorted array.

What is the simplest way of looping an array of id's as posts, but overriding the natural post order for the order that the $sorted array is listed?

$args = array (
    'post__in'  => $sorted, // array of id's that I want in order
    'posts_per_page'    => -1, // so that it shows everything in the array
    'ignore_sticky_posts' => 1 // so that stickys dont affect order
);



$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>n' . get_the_title() . ' - '. get_the_ID () .'</li>';
    }
    echo '</ul>';
} else {
    _e( 'Sorry, no posts matched your criteria.' );
}

wp_reset_postdata();
  • 写回答

1条回答 默认 最新

  • dongqin1819 2016-06-07 03:35
    关注

    You can just use 'orderby' => 'post__in'

    <?php 
    $postsArgs = array(
        "post_type" => "post",
        "orderby" => "post__in",
        "order" => "ASC",
        "posts_per_page" => "-1",
        "post__in" => array(1,8,6)
    );
    $posts = new WP_Query($postsArgs);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器