I was told that get_posts
is a very bad function to use that that making a new WP_query
is absolutely better.
My problem is that my get_posts
code works but when I tried turning it into a WP_query
, it returns blank results. Assistance please? :( I'd really love to get this to work because I suspect that the get_posts that I have on index.php / category-9.php are interfering with the normal loop on category.php
Get_posts version:
<?php if ( have_posts() ) : while ( (have_posts() ) ) : the_post(); ?>
<div class="postcontainer">
<h2><a href="<?php the_permalink();?>"><?php the_title(''); ?></a></h2>
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 4,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => name,
'order' => ASC
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post); ?>
<a class="postpreview" href="<?php echo get_permalink($post->post_parent) ?>"><?php echo wp_get_attachment_image($post->ID, thumbnail) ?></a>
<?php };
}; ?>
<div class="clear"></div>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
WP_query version:
<?php if ( have_posts() ) : while ( (have_posts() ) ) : the_post(); ?>
<div class="postcontainer">
<h2><a href="<?php the_permalink();?>"><?php the_title(''); ?></a></h2>
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 4,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => name,
'order' => ASC
);
$attachments = new WP_Query($args);
while ($attachments->have_posts()) : ?>
<a class="postpreview" href="<?php echo get_permalink($post->post_parent) ?>"><?php echo wp_get_attachment_image($post->ID, thumbnail) ?></a>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clear"></div>
</div>
<?php endwhile; endif; ?>
get_posts()
a bad function? It's just a simple wrapper forWP_Query
. You probably meantquery_posts()
?WP_DEBUG
activated on your test site, to see if you got any errors.WP_DEBUG
! Completely forgot that I disabled that ages ago.'numberposts' => 4,
is deprecated, useposts_per_page
instead.