WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
1  
Have you considered using the pre_get_posts filter to modify the main query instead? It would be significantly faster, an the code would be much smaller/simpler than discarding the main query and reinventing it, doubling the work and page load in the process – Tom J Nowell Apr 7 at 21:57
    
You're right, at that moment my WordPress knowledge was limited, now I know I should have used the pre_get_posts filter. Go ahead an post an answer and I'll mark it as correct, hopefully it will help others to not make the same mistake as I have. – George Grigorita Apr 8 at 13:42

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.