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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?