I got an array of Post Ids
$ids = array(1277,6098,6709, 6098);
I want to loop throw these specific posts with:
$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;
But wordpress automatically excludes the repeated Id (6098). How can I avoid this?
I tryed to create my own function. But unfortunately it doesnt work. I created my own get_posts function like this:
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);
}
I changed the line from:
$incposts = wp_parse_id_list( $r['include'] );
to:
$incposts = $r['include'];
to avoid removing duplicated Ids from array. But this function still doesnt show the duplicated posts from my Id List.
Any Ideas?
You can loop your ids and call get_post
with setup_postdata
:
global $post;
foreach ($ids as $id) :
$post = get_post($id);
setup_postdata( $post );
the_title();
endforeach;