Support » Plugins » Hacks » cant figure out how to convert a query_posts to WP_Query

  • I have a query_posts working, but since its not a custom WP_Query like it needs to be the posts per page setting in the admin affects it. How can I change it to a custom query using WP_Query? I have never done it with displaying attachments. I have with displaying posts from a category like in the codex. I just need to display the latest 6 images from a specific post category (which it does). But my blog settings must be set to “Blog pages show at most 1” for it to work. If I change it to anything else it shows more than 6.


    <?php query_posts( array( 'category__and' => array(1), 'orderby' => 'title', 'order' => 'DESC' ) );
    while ( have_posts() ) : the_post(); ?>
    $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'posts_per_page' => 6, 'order' => 'DESC', 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );
    $images = get_posts($args);
        if ($images) {
        foreach ( $images as $image ) { ?>
          <a href="<?php echo wp_get_attachment_url( $image->ID , 'thumbnail' ); ?>" rel="lightbox"><div class="home-thumbs"><?php echo wp_get_attachment_image( $image->ID , 'thumbnail' ); ?></div><!--end home-thumbs--></a>
    <?php   }
        } ?>
    	<?php endwhile;
    	// Reset Query
    	wp_reset_query(); ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael


    Forum Moderator

    change these lines:

    <?php query_posts( array( 'category__and' => array(1), 'orderby' => 'title', 'order' => 'DESC' ) );
    while ( have_posts() ) : the_post(); ?>


    <?php $image_query = new WP_Query( array( 'category__and' => array(1), 'orderby' => 'title', 'order' => 'DESC', 'posts_per_page' => 1 ) );
    while ( $image_query->have_posts() ) : $image_query->the_post(); ?>

    and use wp_reset_postdata(); instead of wp_reset_query();

    I will try this soon as I get home. I have been trying to figure out how to do this over a week. I am not very familiar with more advanced queries. I will post my results soon as I apply this. Thank you for your help!

    Ok it works! Thank you for showing me this. I have a question about whats going on here. Why does the $args array have some of the same keys as the $image_query ? I dont understand. To me it seems like you could eliminate some of that in one or the other since its some what duplicated right? Would you mind explaining to me if this custom loop is completely correct and not excessive in any way? The thing that gets me is that both array’s have a “posts_per_page”. But only the $image_query has the ‘category_and’, why is that?

    Thanks again, I hope you that you will take the time to explain this to me. I have read through much of the codex and as you know it can be challenging at times, especially for a beginner level php guy.



    Forum Moderator

    these are two independant ‘queries’ so each needs its own set of paramters.

    Are they both necceassry for this function to work? If I try to remove the top query and modify the second one by adding the category parameters and it doesn’t work. That’s why I think this could be cleaned up, or am I wrong. Both are needed to make this work?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘cant figure out how to convert a query_posts to WP_Query’ is closed to new replies.