Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two questions in a row:

  • How to check the array values, what WP_Query has brought to me?
  • If I get 10 posts in cat=3, then I want to get only 5 from them, and then want to echo them individually wherever I want them to print.

It may be like:

-------------------
| 1  |  2         |
-------------------
| 3 | 4 |
-------------------
| 5 |
-------------------

But I don't have any idea on both of the problems.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Assuming you start with:

$custom_query_args = array( 
    'cat' => 10,
    'post_per_page' => 5
);

$custom_query = new WP_Query();

Then to answer your first question, you can see what is returned (for development/debugging) via:

var_dump( $custom_query );

To output your query as a normal loop, you simply need to call a custom loop, using the have_posts() and the_post() member functions of the Query class:

// Start loop
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
    // Loop output goes here
// End loop
endwhile; endif;
// Restore $post data to the default query
wp_reset_postdata();
share|improve this answer
add comment

Thanks all of you for your kind assistance.
We did it with a for loop like this:

<div id="lead_news">
<div id="lead_news_left">
<?php if ( have_posts() ) : ?>
<?php
$lead = new WP_Query( 'cat=3' );

// IT WILL ECHO POST NO. 1 HERE, ONLY
for ( $counter = 0; $counter <= 1; $counter += 1) {

$lead->the_post(); ?>

<?php get_template_part( 'content', get_post_format() );

$counter++;
}
?>
</div> <!-- #lead_news_left -->

<div id="lead_news_right">
<div id="lead"><strong>Other Lead News:</strong></div>

<ul id="lead_other">

<?php
// IT WILL ECHO POST NO. 2 to 6 HERE
for ( $counter = 1; $counter <= 6; $counter += 1) {

$lead->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a>
</li>

<?php } ?>

</ul>  <!-- #lead_other -->

</div> <!-- #lead_news_right -->

</div> <!-- #lead_news -->

Then when my query ends, I ended it as usual with:

<?php
// Reset Post Data
wp_reset_postdata();
?>

And after everything, we ended with an <?php endif; ?>.

I agree that, It's not what I asked here, but it worked for me, and I can display posts wherever I want 'em to display. :)

It displays like:

----------------
POST 1 | POST 2
       | POST 3
       | POST 4
       | POST 5
       | POST 6
----------------

Thanks all of you again for your assistance and precious time.
Yap, CODE IS POETRY!

share|improve this answer
add comment

The posts retrieved from a query are stored in the variable named posts. You can read or modify that array as you wish. For example if the WP_Query object is $query, you can do $posts = $query->posts;

For your next question, just don't use the while & display them wherever you like

if(have_posts()) {
    the_post();
    the_title();
}

// some other code

if(have_posts()) {
    the_post();
    the_title();
}

the_post() sets the current post to the next post in the objects posts array. The rest is just pure php

share|improve this answer
    
I would not recommend conflating core-defined query variables with custom queries. It just leads to problems. –  Chip Bennett Nov 24 '12 at 13:39
    
As long as you not touch the global $wp_query & also take care of all the other object variables such as post_count, modifying posts variable works fine. WP_Query class is flexible enough for that & in case we ever reach a use case where that's not enough, we can extend the class to suit our needs. :) –  Mridul Aggarwal Nov 24 '12 at 13:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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