0

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; ?>
7
  • 1
    Why is get_posts() a bad function? It's just a simple wrapper for WP_Query. You probably meant query_posts()?
    – birgire
    May 1, 2014 at 12:38
  • @birgire I actually wouldn't really know :/ It was an admonishment from someone in #wordpress@freenode and I wasn't able to ask why. I'm actually just trying it because I feel that it's the reason why my normal loop on another template file isn't working properly.
    – Arkuen
    May 1, 2014 at 12:39
  • It must have been some misunderstanding ;-) Always test your code with WP_DEBUG activated on your test site, to see if you got any errors.
    – birgire
    May 1, 2014 at 12:47
  • I don't know D: They very clearly said "YOU'RE USING GET POSTS. BAD." Thanks for WP_DEBUG! Completely forgot that I disabled that ages ago.
    – Arkuen
    May 1, 2014 at 12:51
  • 'numberposts' => 4, is deprecated, use posts_per_page instead. May 1, 2014 at 13:21

1 Answer 1

0

You forget $query->the_post(), which makes the same as setup_postdata(). Also you are using $post->ID inside the loop but $post is not current post object in the loop. You have also syntax errors; for example you pass thumnail to wp_get_attachment_image() instead of a string "thumbnail", and pass name as orderby argument instead of the string "name", or ASC instead of "ASC":

 <?php
 //as you are inside a loop but I don't see $post variable in your code,
 //I assume you want the current post in the loop as parent
 $parent_id = get_the_ID();

 $args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => 4,
    'post_status' => 'inherit',
    'post_parent' => $parent_id,
    'orderby' => 'name',
    'order' => 'ASC'
);

$attachments = new WP_Query($args);
while ($attachments->have_posts()) {
       $attachments->the_post();
       //I pass $parent_id to get_permalink() because you was trying
       //to do that in your code but I'm not sure if this is what you want
       ?>
      <a class="postpreview" href="<?php echo get_permalink($parent_id); ?>"><?php echo wp_get_attachment_image(get_the_ID(), 'thumbnail') ?></a>

 <?php
 }
 wp_reset_postdata();
 ?>

Anyway, new WP_query is not much better than get_posts(), each one has its benefits and the better depends on what you want to do.

7
  • Hi there! Thanks for answering. Unfortunately, I tested it and replaced $post->ID with $postid (defined using get_the_ID()) but it still returns blank.
    – Arkuen
    May 1, 2014 at 12:33
  • There was some syntax errors I copied from your code, see edited answer.
    – cybmeta
    May 1, 2014 at 12:37
  • Gave it a shot but still nothing :( Really barking odd because even the HTML (like the link HTML) isn't displaying in source so it must not be working at all...
    – Arkuen
    May 1, 2014 at 12:43
  • Try var_dump($attachments) after the query, and let's know what's it showing. May 1, 2014 at 13:23
  • @MayeenulIslam Outputs a ton of blocks. Here's the first one: pastebin.com/uU5MyQ2m
    – Arkuen
    May 1, 2014 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.