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 am using custom post type. But on landing page Count of post loop is not in order

<?php $loop = new WP_Query( array( 'post_type' => 'focus-areas' ,'get' => 'all') );  ?>

        <?php while ( $loop->have_posts() ) : $loop->the_post(); $counts++; ?>
<?php echo $counts; ?>

This printing as 16, 1, 3, 4, 5,....

All the post are getting display but it's not in order. i searched in google and wordpress site but din't get any solution

share|improve this question
1  
get is not a valid parameter for WP_Query. That does nothing. Where did you get that? –  s_ha_dum Jun 7 '13 at 13:32
add comment

1 Answer 1

up vote 2 down vote accepted

a) Try to initialize the $count variable before your loop:

$counts=0;
$loop = new WP_Query( array( 'post_type' => 'focus-areas', 'posts_per_page' => -1 ) );  
while ( $loop->have_posts() ) : $loop->the_post(); 
    $counts++; 
    echo $counts;
    echo " - "; 
endwhile;

b) or use the current_post method (starts at 0) of WP_Query:

$loop = new WP_Query( array( 'post_type' => 'focus-areas', 'posts_per_page' => -1 ) );  
while ( $loop->have_posts() ) : $loop->the_post(); 
    echo $loop->current_post; 
    echo " - "; 
endwhile;

Results:

On my install where I have 12 posts in a custom post type, case a) gives me:

1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 -

and case b) gives:

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 -

ps: I'm not sure what you mean by 'get' => 'all' but the usual way of fetching all posts is to use 'posts_per_page' => -1

share|improve this answer
    
'get' => 'all' means i get all the post from the post type. Wordpress can display 10 post by default. i tried echo $loop->current_post; but still same result; –  Mikul Gohil Jun 7 '13 at 10:20
    
the usual way is to use 'posts_per_page' => -1 to get all posts. I updated the answer with my results. –  birgire Jun 7 '13 at 10:44
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.