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 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); 

        $wp_query->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>

        </li>    
        <?php endwhile; ?>

        <nav>
         <?php paginate(); ?>
        </nav>

        <?php 
         $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

 
discard

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

Browse other questions tagged or ask your own question.