Henrik Sandstrom
Henrik Sandstrom

Reputation: 21

Wordpress - Get position in query

I have a wordpress website with a static frontpage. I show ten posts on my frontpage and they are sorted by their amount of votes, using the following query:

<?php 
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
query_posts(
$args=array(  
    'post_type' => 'post',
    'order'     => 'DESC',
    'meta_key' => 'cf_votes',
    'orderby'   => 'meta_value_num',
    'posts_per_page' => 10,
    'paged' => $paged

)
);

$my_query = null;
$my_query = new WP_Query($args);
?>

And then I get a posts current position in the query with this simple code:

<?php $count = $my_query->current_post; echo $count; ?>

And this works perfectly on the first page. The posts are displayed with their current ranking 1, 2 , 3 - 10. However, I also use pagination. So I can browse for the next 10, 20, 30 posts in the query and so on. Now if I click on "Next", another 10 posts will be displayed. But even if they are number 11, 12, 13 -20 in the query, it shows that they are 1, 2, 3 -10. So it basically starts over, and never surpasses 10.

How can I define $count so that it continues numbering from where it left off on the previous page?

Upvotes: 0

Views: 570

Answers (1)

rnevius
rnevius

Reputation: 27102

You could use $paged to modify the $count...Something like:

$current_index = $my_query->current_post;
$count = ( intval($paged) * 10 - 10 ) + intval( $current_index ); 
echo $count;

So, for example, if you're on page 2, $paged == 2. So the math would work out like the following (for the first post on the page):

$count = ( 2 * 10 - 10 ) + ( 1 );
$count = 10 + 1;
$count = 11;

Upvotes: 1

Related Questions