Skip to content
Advertisement

Custom WP_Query ‘posts_per_page’ is not working

I’m having trouble with multiple posts showing when I’ve declared ‘posts_per_page’.

During my research my code has developed but with still no results. I’ve tried

'ignore_sticky_posts' => 1, 'nopaging' => true (pagination)

switching theme and deactivating plugins. I can’t seem to find the issue. Any help would be much appreciated. Here is the current result – alderneyfootball.co.uk

I have two loops on the page and it’s a custom page template. I’m using the WP-Club-Manager Plugin

<?php 
     // the query
     wp_reset_query();

     $wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1 );

     $wpb_next_game = new WP_Query($wpb_next_game);
      if ( have_posts() ) : while ( $wpb_next_game->have_posts() ) : $wpb_next_game->the_post();

             $date = date_i18n( get_option( 'date_format' ), strtotime( $post->post_date ) );
             $time = date_i18n( get_option( 'time_format' ), strtotime( $post->post_date ) );
             $side = wpcm_get_match_clubs( $post->ID );

             // badge
             $badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'home-logo' ) );
             $badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'away-logo' ) );
             $format = get_match_title_format();
             if( $format == '%home% vs %away%') {
                $badge = $badges[0];
             } else {
                $badge = $badges[1];
             } 
             ?>

        <div id="next-match" class="row">
         <div class="span_6 col">
            <h3 class="next-match-home"><?php echo $side[0]; ?></h3>
         </div>
         <div class="span_6 col">
            <h3 class="next-match-away"><?php echo $side[1]; ?></h3>
         </div>
      </div>
         <div class="row">
            <h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4>
         <?php endwhile; ?>
         <!-- end of the loop -->
     <?php else : ?>
        <h4 id="next-match-time"><?php _e( 'Sorry, scheduled matches' ); ?></h4>
     <?php endif; ?>

Advertisement

Answer

If the two loops data is overwridden then, i your first code wp_reset_query() is incorrect. If you are using WP_Query then

wp_reset_postdata() //remove wp_reset_query() which is used for wp_query()

should be used after the end of the WHILE loop which means that in your two loops you have to have

wp_reset_postdata()  // use this at both loops

at both loops in the end of the while loop.

Now you codes looks like this:

<?php 

 $wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1 );

 $wpb_next_game = new WP_Query($wpb_next_game);
  if ( have_posts() ) : while ( $wpb_next_game->have_posts() ) : $wpb_next_game->the_post();

         $date = date_i18n( get_option( 'date_format' ), strtotime( $post->post_date ) );
         $time = date_i18n( get_option( 'time_format' ), strtotime( $post->post_date ) );
         $side = wpcm_get_match_clubs( $post->ID );

         // badge
         $badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'home-logo' ) );
         $badges = wpcm_get_match_badges( $post->ID, 'crest-medium', array( 'class' => 'away-logo' ) );
         $format = get_match_title_format();
         if( $format == '%home% vs %away%') {
            $badge = $badges[0];
         } else {
            $badge = $badges[1];
         } 
         ?>

    <div id="next-match" class="row">
     <div class="span_6 col">
        <h3 class="next-match-home"><?php echo $side[0]; ?></h3>
     </div>
     <div class="span_6 col">
        <h3 class="next-match-away"><?php echo $side[1]; ?></h3>
     </div>
  </div>
     <div class="row">
        <h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4>
     <?php endwhile; 
     wp_reset_postdata();
        ?>
     <!-- end of the loop -->
 <?php else : ?>
    <h4 id="next-match-time"><?php _e( 'Sorry, scheduled matches' ); ?></h4>
 <?php endif; ?>

Hope this works for you

Thank You

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement