[wp-testers] possible bug in get_posts
Daniel Tweedy
daniel.tweedy at gmail.com
Mon Jan 31 20:27:50 UTC 2011
I tried the code below with the wp_reset_postdata() it seemed to work great, until I put a second get_posts loop in the page, after that the 2nd one always showed what the first one did despite me change the post_type in the query.
This is the actual code I was using.
$posts = '';
// Set options for post query
$post_args = array(
'orderby' => $sort_column,
'order' => $sort_order,
'numberposts' => $limit,
'post_type' => 'post',
);
wp_reset_postdata();
$theposts = get_posts($post_args);
if (count($theposts)> 0) {
foreach($theposts as $post) {
setup_postdata($post);
$posts .= '<li><a href=' . get_permalink() . ' title="' . sprintf(esc_attr__('Permalink to %s'), the_title_attribute('echo=0')) . '" rel="bookmark">' . get_the_title() .'</a></li>';
}
$posts_header = (empty($posts_header)) ? __('Posts') : $posts_header;
$posts = '<div id="wp-realtime-sitemap-posts"><h3>' . $posts_header .'</h3><ul>' . $posts .'</ul></div>';
}
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');
foreach ($post_types as $post_type) {
$custom_post_type_posts = '';
// Set options for query
$post_args = array(
'orderby' => $sort_column,
'order' => $sort_order,
'numberposts' => $limit,
'post_type' => $post_type->name,
);
wp_reset_postdata();
$theposts = get_posts($post_args);
if (count($theposts)> 0) {
foreach($theposts as $post) {
setup_postdata($post);
$custom_post_type_posts .= '<li><a href=' . get_permalink() . ' title="' . sprintf(esc_attr__('Permalink to %s'), the_title_attribute('echo=0')) . '" rel="bookmark">' . get_the_title() .'</a></li>';
}
$custom_post_types[$post_type->name] = '<div id="wp-realtime-sitemap-' . strtolower($post_type->name) . '"><h3>' . $post_type->labels->name . '</h3><ul>' . $custom_post_type_posts .'</ul></div>';
}
}
wp_reset_postdata();
------------------------------
Message: 2
Date: Sun, 30 Jan 2011 17:04:38 -0700
From:michael at mfields.org
Subject: Re: [wp-testers] possible bug in get_posts
To:wp-testers at lists.automattic.com
Message-ID:
<487e6bd9d2e9865bf92bf21f9a2d61f4.squirrel at box267.bluehost.com>
Content-Type: text/plain;charset=iso-8859-1
I don't think that there is a bug in core. Your code looks a bit off to me
though. Please try the following:
$movies = get_posts( array(
'numberposts' => '-1',
'post_type' => 'movies',
) );
/* Backup global $post object !IMPORTANT! */
$_post_backup = $post;
foreach( (array) $movies as $post ) {
setup_postdata( $post );
print '<li><a href=' . get_permalink() . ' title="' .
sprintf(esc_attr__('Permalink to %s'), the_title_attribute('echo=0')) .
'" rel="bookmark">' . get_the_title() .'</a></li>';
}
/* Restore global $post object !IMPORTANT! */
$post = $_post_backup;
Best wishes,
-Mike
More information about the wp-testers
mailing list