0

我有一系列帖子 ID

$ids = array(1277,6098,6709, 6098);

我想循环抛出这些特定的帖子:

$args = array(   'orderby' => 'post__in',
                    'post__in' => $ids);

get_posts($args);
$custom_posts = get_posts($args);
foreach( $custom_posts as $post ) : setup_postdata($post);
    the_title();
    ...
endforeach;

但是 wordpress 会自动排除重复的 Id (6098)。我怎样才能避免这种情况?


我试图创建自己的功能。但不幸的是它不起作用。我创建了自己的 get_posts 函数,如下所示:

function get_posts_jt($args = null) {
                $defaults = array(
                    'numberposts' => 5, 'offset' => 0,
                    'category' => 0, 'orderby' => 'post_date',
                    'order' => 'DESC', 'include' => array(),
                    'exclude' => array(), 'meta_key' => '',
                    'meta_value' =>'', 'post_type' => 'post',
                    'suppress_filters' => true
                );

                $r = wp_parse_args( $args, $defaults );
                if ( empty( $r['post_status'] ) )
                    $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
                if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
                    $r['posts_per_page'] = $r['numberposts'];
                if ( ! empty($r['category']) )
                    $r['cat'] = $r['category'];
                if ( ! empty($r['include']) ) {
                    $incposts = $r['include'];
                    $r['posts_per_page'] = count($incposts);  // only the number of posts included
                    $r['post__in'] = $incposts;
                } elseif ( ! empty($r['exclude']) )
                    $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );

                $r['ignore_sticky_posts'] = true;
                $r['no_found_rows'] = true;

                $get_posts = new WP_Query;
                return $get_posts->query($r);

            }

我改变了这一行:

$incposts = wp_parse_id_list( $r['include'] );

至:

$incposts = $r['include'];

以避免从数组中删除重复的 ID。但是这个功能仍然没有显示我的 ID 列表中的重复帖子。

有任何想法吗?

4

2 回答 2

2

您可以循环您的 id 并get_post使用以下命令调用setup_postdata

global $post;
foreach ($ids as $id) :
    $post = get_post($id);
    setup_postdata( $post );
    the_title();
endforeach;
于 2016-03-04T19:49:47.883 回答
0

您不能:get_posts用于wp_parse_id_list从提供的数组中删除重复的 id。

于 2013-05-29T00:58:31.717 回答