• I have a athlete profile page that pulls in that athletes results. I would like to limit the results to only 5 per page and then have pagination to go to older results. I have pagination that works on pages but will not work for this. Please take a look at the code below and let me know where I am going wrong.

    <section class="col-sm-12">
    
        <?php
            $athlete = single_post_title('', false);
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
            $args = array(
              'post_type'       =>   'result',
              'orderby'         =>   'date',
              'order'           =>   'DESC',
              'posts_per_page'  =>    5,
              'paged'           =>    $paged,
                'meta_query'      =>    array(
                  array(
                      'key'     =>  'result-athlete', // athlete results name
                      'value'   =>  $athlete, // athlete name
                      'compare'  =>  '='
                    ),
                  ),
              );
    
          $the_query = new WP_Query( $args );
    
        ?>
    
        <div class="col-md-12 index-results-Section">
    
    <h3>Results </h3>
    
          <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
          <div class="index-resultsSingle">
             <div class="col-md-2 index-resultsAthlete">
                <?php the_field( 'result-athlete' ); ?>
              </div><!-- .index-resultsAthlete -->
    
              <div class="col-md-7 index-resultsSport">
               <?php the_field( 'result-sport' ); ?>
               <?php the_field( 'result-date' ); ?>
              </div> <!-- .index-resultsSport -->
    
              <div class="col-md-3 index-resultsResult">
                <?php the_field( 'result-result' ); ?>
              </div> <!-- .index-resultsResult -->
    
              <div class="clearfix"></div>
    
           </div>
    
          <?php endwhile; else: ?>
    
                  <h4>There are no results for this athlete at this time.</h4>
    
                <?php endif; ?>
    
                <div class="row"> <!-- Next and Previous -->
                  <?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
                    <nav class="prev-next-posts">
                      <div class="col-md-4 prev-posts-link">
                        <?php echo next_posts_link( '< Older Press Releases', $the_query->max_num_pages ); // display older posts link ?>
                      </div>
                      <div class="col-md-4 col-md-offset-4 next-posts-link">
                        <?php echo previous_posts_link( 'Newer Press Releases >' ); // display newer posts link ?>
                      </div>
                    </nav>
                  <?php } ?>
                </div>
    
        <div class="clearfix visible-sm"></div>
    
        </div>
    
      </section><!-- .col-sm-12 -->

    Any help would be greatly appreciated. Thank you in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m curious to know what results DO you get? 5 Athlete’s stats and then nothing else? No pagination?

    I use posts_per_page to limit how many results (total) I get….I’m thinking that *could* be your problem….

    BUT try this:

    Add to the bottom where you have your pagination:
    $total_pages = $the_query->max_num_pages; if($total_pages >1) {

    then continue on with what you have. I’m hoping that will work.

    Thread Starter Vince_M

    (@vince_m)

    I don’t get any results at all. I don’t get older posts. I did Inspect Element and the row is there but the row is empty.

    I tried the code above but it threw and error on the line two lines below it.

    I have even tried a plugin but nothing will show up. Not sure what is wrong here.

    OK sorry I didn’t catch this before, I was hung up on your pagination, not that you’re not getting results.

    SO to help me understand, you have two custom post types? 1) Athletes and 2) Results, and you’re wanting to display related Results on the Athletes pages by matching on the athlete’s name(s)?

    Now, the *easy* way to do this is to use the Advanced Custom Fields Pro plugin, which supports relationship fields. I only suggest it because it makes doing what you want to do pretty simple, BUT that said, there are several ways to accomplish that without ACF Pro, they’re just trickier.

    That said, the first thing you should try is changing “single_post_title()” to “get_the_title()” – single_post_title is use primarily for display, get_the_title can be returned and used in queries as a variable.

    And of course your title (the athlete’s name) must match exactly what is in the Result’s post’s meta key for athlete-name perfectly.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘pagination with dynamically pulled in posts’ is closed to new replies.