WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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 am trying to Order custom post type results by a meta_key value with pagination. The pagination is working fine and I suspect I can't use the same $args that I am using with my new WP_Query in my wp_query->query(). Here is the code :

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $temp = $wp_query; 
        $wp_query = null; 
        $args = array(
          'showposts'=> 5,
          'orderby'  => 'meta_value',
          'meta_key' => 'report_date',
          'order' => 'DESC',            
          'post_type'=> 'match_report',
          'paged'    => $paged
        $wp_query = new WP_Query($args); 

        if ( $wp_query->have_posts() ) :

        <ul class="grid">
        <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
        $matchdate = get_post_meta($post->ID, 'report_date', true);
        $matchDay = date("d", strtotime($matchdate));
        $matchMonth = date("M", strtotime($matchdate));
        $hometeam = get_post_meta($post->ID, 'report_home-select', true);
        $homename = get_the_title($hometeam);               
        $awayteam = get_post_meta($post->ID, 'report_away-select', true);
        $awayname = get_the_title($awayteam);
        $permalink = get_permalink($post->ID); 

        <li class="module-left grid"><div class="date col-one-fourth"><h4><?php echo $matchDay .' ' .'<span>' . $matchMonth . '</span>' ?></h4></div><h2 class="matchlist col-two-fourths"><?php echo $homename . " vs. " . $awayname;?></h2>
        <div class="matchbutton col-one-fourth"><a href="<?php echo $permalink ?>"><button>Get Report</button></a></div>

        <?php endwhile; ?>

         <?php paginate(); ?>

         $wp_query = null; 
         $wp_query = $temp;  // Reset
share|improve this question
Don't set the main $wp_query object to null. Just save it into for example: $new_query and access that one later on - $new_query->have_posts(), etc. – kaiser Jul 8 '13 at 10:00
hmm, tried that and I couldn't get it to work. It kept saying that my $new_querywas undefined. I just took out the $argsfrom the new WP_Queryand it seems to be working now. Thank you for your input. – mantis Jul 8 '13 at 10:55
possible duplicate of How to fix pagination for custom loops? – Chip Bennett Oct 31 '13 at 14:39

Your Answer


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

Browse other questions tagged or ask your own question.