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

时间:2016-06-07 03:00:26

标签: php wordpress list loops

我有一个帖子ID列表,我想将帖子循环播放,我已尝试将WP_QUERY与post__in一起使用,但这并未保留id列出的顺序在$ sorted数组中。

将id数组作为帖子循环的最简单方法是什么,但是为了列出$ sorted数组的顺序覆盖自然的post顺序?

$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 个答案:

答案 0 :(得分:1)

你可以使用&#39; orderby&#39; =&GT; &#39; post__in&#39;

<?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);
?>