Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Custom WP_Query 'posts_per_page' is not working

Writer Olivia Zamora

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> <div> <h3><?php echo $side[0]; ?></h3> </div> <div> <h3><?php echo $side[1]; ?></h3> </div> </div> <div> <h4>Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4> <?php endwhile; ?> <!-- end of the loop --> <?php else : ?> <h4><?php _e( 'Sorry, scheduled matches' ); ?></h4> <?php endif; ?>

3 Answers

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> <div> <h3><?php echo $side[0]; ?></h3> </div> <div> <h3><?php echo $side[1]; ?></h3> </div> </div> <div> <h4>Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4> <?php endwhile; wp_reset_postdata(); ?> <!-- end of the loop --> <?php else : ?> <h4><?php _e( 'Sorry, scheduled matches' ); ?></h4> <?php endif; ?>

Hope this works for you

Thank You

2

Perhaps your next loop takes query from previous. Make sure you're using wp_reset_query() for query_posts and/or wp_reset_postdata() for wp_query

After each loop.

I was facing same issue. If you have added below in code then remove it and it will work for you.

wp_reset_postdata() 

Thanks

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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