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 a problem with a custom WP_Query. I want a page with all posts with a specific tag. This is the code I'm using:

<?php 
//save old query
$temp = $wp_query; 
//clear $wp_query; 
$wp_query= null; 
 ?>      
<?php 
// The Query
$wp_query = new WP_Query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'tag' => 'concurs', 'paged' => $paged );
$wp_query->query($args);
?>
<?php 
// The Loop
while ( $wp_query->have_posts() ) :
    $wp_query->the_post();
 ?>  
[some code that displays the title, excerpt, meta-info (author, category, date) and thumbnail] 
      <?php endwhile; ?>
<?php posts_nav_link(); ?>              
<?php 
//clear again
$wp_query = null; 
//reset
$wp_query = $temp;  
 ?> 

The problem is that when I switch to page 2 or 3 I get the same 10 first posts. What I am doing wrong?

share|improve this question

2 Answers 2

Your query has no paging.

You need to tell the query what page your on so it can get the right results for you. You can grab the 'paged' query_var for this.

share|improve this answer
    
Thanks, I've included the paged query_var in the code but it's still not working [code updated above]. –  George Grigorita Feb 3 '13 at 15:37

Finally I've figured out the correct coding for this problem:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query('tag=concurs&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
?>
<?php if ($wp_query->have_posts()) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>  
[code for title, author, category, meta-info etc] 
<?php endwhile; ?>
 <div class="next-prev"><?php posts_nav_link( ' ', '<img src="' . get_bloginfo('stylesheet_directory') . '/img/pagina-precedenta.png" align="left" />', '<img src="' . get_bloginfo('stylesheet_directory') . '/img/pagina-urmatoare.png" align="right" />'); ?></div>  
<?php endif; ?>         
share|improve this answer

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.