0

So, it's taken me most of the day to get my query working (some days I feel like I don't know WP at all) and then I was getting a 404 error on paginated pages which I've since fixed but now I'm going to "page 2" but the loop doesn't seem to be working.

Here's my index.php query (cobbled together from another SE post):

<?php
$do_not_duplicate=0;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
if(!is_paged()):
  //Get featured content for page 1
  $pagecount = $my_query->max_num_pages;
  $my_query = new WP_Query('category_name=featured&posts_per_page=50');
  while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;?>
    <div class="row event-row">
        <div class="training-meta small-12 large-3 columns">
            <?php 
                // get raw date
                $training_date = get_field('training_date', false, false);
                // make date object
                $training_date = new DateTime($training_date);
            ?>
            <h4 class="training-meta-date"><?php echo $training_date->format('M d, Y'); ?> <span>at <?php the_field('training_time'); ?></span></h4>
            <hr />
            <?php the_field('training_location'); ?>
            <a class="register-button" href="<?php echo the_field('registration_url'); ?>">Register Now</a>
        </div><?php /*== //.training-meta ==*/ ?>
        <div class="training-details small-12 large-8 columns">
            <h3 class="event-title"><?php the_title(); ?></h3>
            <span class="inline-date"><?php echo $training_date->format('M d, Y'); ?></span> <?php the_field('training_details'); ?>
        </div><?php /*== //.training-details ==*/ ?>
    </div><?php /*== //.row .event-row ==*/ ?>

<?php endwhile; endif; ?>
    <div class="row">
        <div class="small-12 large-12 large-centered">
            <h3 class="page-entry-title upcoming">Past Training Sessions</h3>
        </div>
    </div>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
    <div class="training-details small-12 large-6 large-centered">
        <h3 class="event-title"><?php the_title(); ?></h3>
        <span class="inline-date"><?php echo $training_date->format('M d, Y'); ?></span> <?php the_field('training_details'); ?>
    </div><?php /*== //.training-details ==*/ ?>
<?php endwhile; endif; ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( function_exists( 'foundationpress_pagination' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
        <nav id="post-nav">
            <div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
            <div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
        </nav>
    <?php } ?>

And here's my pre_get_posts function (pagination wouldn't work without this):

    function training_homepage_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post' ) );
    }
}
add_action( 'pre_get_posts', 'training_homepage_posts' );

And here's my dev URL for review so you can see what's happening. Basically, when I paginate, I want the same featured posts to show up on the second page but with the past events showing below it (though I'd be happy just to get the pagination showing the rest of the past events content beyond the title).

A push in the right direction would be greatly appreciated. Client is getting antsy for me to resolve this (short turn-around on project), as am I.

TIA!

EDIT TO SHOW CORRECTED CODE:

<?php
$do_not_duplicate=0;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
  //Get featured content for page 1
  $pagecount = $my_query->max_num_pages;
  $my_query = new WP_Query('category_name=featured&posts_per_page=50');
  while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;?>
    <div class="row event-row">
        <div class="training-meta small-12 large-3 columns">
            <?php 
                // get raw date
                $training_date = get_field('training_date', false, false);
                // make date object
                $training_date = new DateTime($training_date);
            ?>
            <h4 class="training-meta-date"><?php echo $training_date->format('M d, Y'); ?> <span>at <?php the_field('training_time'); ?></span></h4>
            <hr />
            <?php the_field('training_location'); ?>
            <a class="register-button" href="<?php echo the_field('registration_url'); ?>">Register Now</a>
        </div><?php /*== //.training-meta ==*/ ?>
        <div class="training-details small-12 large-8 columns">
            <h3 class="event-title"><?php the_title(); ?></h3>
            <span class="inline-date"><?php echo $training_date->format('M d, Y'); ?></span> <?php the_field('training_details'); ?>
        </div><?php /*== //.training-details ==*/ ?>
    </div><?php /*== //.row .event-row ==*/ ?>

<?php endwhile; ?>
    <div class="row">
        <div class="small-12 large-12 large-centered">
            <h3 class="page-entry-title upcoming">Past Training Sessions</h3>
        </div>
    </div>
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
    <div class="training-details small-12 large-6 large-centered">
        <h3 class="event-title"><?php the_title(); ?></h3>
        <span class="inline-date"><?php echo $training_date->format('M d, Y'); ?></span> <?php the_field('training_details'); ?>
    </div><?php /*== //.training-details ==*/ ?>
<?php endwhile; endif; ?>

    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( function_exists( 'foundationpress_pagination' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
        <nav id="post-nav">
            <div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
            <div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
        </nav>
    <?php } ?>
  • 1
    With is_paged you are testing whether a post has been divided in parts using the <!--nextpage--> QuickTag. But with next_posts_link you are pointing to the next page of posts. Which one are you looking for? – cjbj May 18 '16 at 8:27
  • next_posts_link I'll edit the query and see if that makes a difference. – Justice Is Cheap May 18 '16 at 16:15
  • Glad to help. I'll post it as an answer, so you can accept it and this question is marked as solved. – cjbj May 18 '16 at 17:54
1

With is_paged you are testing whether a post has been divided in parts using the QuickTag. But with next_posts_link you are pointing to the next page of posts. So you probably need another test.

  • Thanks again. I probably would have spent hours trying to figure that out. – Justice Is Cheap May 19 '16 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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