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.

This question already has an answer here:

I'm having a huge problem a custom query within a custom post type template with regards to pagination.

For some reason, the previous post, next post links are showing blank. If I do "www.domainname.com/wolf/spot/paged/2/ it shows a 404 page.

The code is below... as I said above, this is a custom query, WITHIN a custom post type template.

<div id="content">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class="post" id="post-<?php the_ID(); ?>">
        <h1 class="pageName"><?php the_title(); ?></h1>
        <div class="entry">
            <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>

            <!-- BEG Wolf Logs -->

            <h3>Recent Logs for <?php the_title(); ?>:</h3>

            <?php
            wp_reset_postdata();
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            if (isset($_GET['show'])) {
                $ppp = $_GET['show'];
            } else {
                $ppp = 10;
            }

            if (isset($_GET['archyear'])) {
                $archYear = $_GET['archyear'];
            } else {
                $archYear = date("Y");
            }

            $wolf = strtolower(get_the_title());
            $logArgs = array(
                        'post_type' => 'wolf-log',
                        'post_status' => 'publish',
                        'orderby' => 'meta_value_num',
                        'meta_key' => 'observationDate',
                        'posts_per_page'=> $ppp,
                        'meta_query' => array(
                                            'relation' => 'AND',
                                            array(
                                                'key' => 'wolf',
                                                'value' => $wolf,
                                                'compare' => '='
                                            ),
                                            array(
                                                'key'       => 'observationDate',
                                                'type'      => 'numeric',
                                                'value'     => array($archYear.'0101',$archYear.'1231'),
                                                'compare'   => 'BETWEEN'
                                            )
                                        ),
                        'order' => 'DESC',
                        'paged' => $paged
            );


            $wlQuery = new WP_Query($logArgs);

            if ($wlQuery->have_posts()) {
                while ($wlQuery->have_posts()) {
                    $wlQuery->the_post();
                    get_template_part('loop', 'wolflogs');
                } // endwhile


            ?>

            <div id="postFooter">
                <div class="navigation">
                    <p><?php next_posts_link('&lt; Older Posts', $wlQuery->max_num_pages); ?></p>
                    <p><?php previous_posts_link('Newer Posts &gt;', $wlQuery->max_num_pages); ?></p>
                </div>
            <?php   
            } else {
                echo '<h2>No current logs, check back later.</h2>';
            }

            // Restore original Post Data
            wp_reset_postdata();
            ?>
            <div class="pageControls">
                <?php
                    $archArray = array();
                    $archArgs = array(
                                'post_type'         => 'wolf-log',
                                'post_status'       => 'publish',
                                'meta_key'          => 'wolf',
                                'meta_value'        => $wolf,
                                'order'             => 'DESC',
                                'posts_per_page'    => -1
                                );

                    $archQuery = new WP_Query($archArgs);

                    if ($archQuery->have_posts()) {
                        while($archQuery->have_posts()) {
                            $archQuery->the_post();
                            $obDate = get_post_meta($post->ID, 'observationDate', true);
                            $obDate = strtotime($obDate);
                            $obDate = date("Y", $obDate);
                            $archArray[$obDate] = $obDate;
                        }
                    } else {
                        echo 'No Archives';
                    }

                    wp_reset_postdata();
                    ?>

                    <div class="styled-select">

                        <select name="archivesPager" id="archivesPager">
                            <option value="">Archive</option>
                            <?php
                                arsort($archArray);
                                foreach($archArray as $archYear) {
                                    echo '<option value="?archyear='.$archYear.'">'.$archYear.'</option>';
                                }
                            ?>

                        </select>
                    </div>
                </div>
                <div class="clearFix"></div>
            </div><!-- END postFooter -->

            <?php
            // Restore original Post Data
            wp_reset_postdata();

            ?>
            <!-- END Wolf Logs -->

        </div>
    </div>
<?php endwhile; else: ?>

    <p>Sorry, no posts matched your criteria.</p>
</div>
share|improve this question

marked as duplicate by Chip Bennett, Johannes Pille, kaiser, toscho Oct 31 '13 at 14:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Well for one thing you have wp_reset_postdata everywhere. I see it being called after the start of loop why? Try removing all instances of that and put it at the end once after the endwhile statement and before the else: –  splashingpixels Sep 4 '13 at 18:38
    
That was a desperate attempt to fix it. I just removed it and it still is no-go. –  Troy Sep 4 '13 at 18:44
    
Also since you're building your own custom loop, why do you still need the standard loop wrapping it? –  splashingpixels Sep 4 '13 at 18:49
    
Good Question. The Standard Loop is a "Wolf Profile" page, which has info on the wolf itself. The custom loop is for a different custom type called a "Wolf Log" which is basically journal entries for that particular wolf. Hence, the need for one within another. –  Troy Sep 4 '13 at 18:50
    
Try putting your next_posts_link and previous_posts_link OUTSIDE of your custom while loop –  splashingpixels Sep 4 '13 at 18:56

1 Answer 1

Also, this code is working fine on a standard "page" template, just not on the single-wolf.php template.

Paging does not work in a single template page. Single pages use the <!--next page--> tag to use paging.

The global variables used are different. The global variable $page is used in a single post (or page) to do paging instead of $paged which is used with other pages.

The next and previous link functions rely on the $paged global variable. Sometimes you can trick WordPress by setting $paged to the value of $page:

$paged = $page;

Place it above this line:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

EDIT:

You may also need to change the query variable here:

'paged' => $paged

TO:

'page' => $paged
share|improve this answer
    
I'm looking at paging the sub-query. I'm going to have to try something else. –  Troy Sep 4 '13 at 21:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.