• I manage a news site that has several transients set on the home page for different content areas. This has been working well for us, but recently (since our update to PHP7 and WP5 in December), at least twice a day, the posts_per_page argument seems to be ignored, and 10 items are returned. Deleting the transients associated with those queries seems to fix the issue.

    Anybody have any idea what this could be? I can’t find any obvious errors when this happens; it just returns ten results instead of what we ask. We don’t use sticky_posts. Here’s a sample:

    <?php 
    // Commentary
    if( false === ( $commentary_query = get_transient( 'commentary_query' ) ) ) {
                    $commentary_query = new WP_Query( [ 'no_found_rows'=>true, 'cat'=>'29','post_type' => 'post', 'post_status'=>'publish','posts_per_page'=>4 ] );
    
     set_transient( 'commentary_query', $commentary_query, 60*60*4 );
    }                       
    while ( $commentary_query->have_posts() ) :
        $commentary_query->the_post();
            echo '<li><a href="' .get_permalink().'"><h4>' . get_the_title() . '</h4><span class="excerpt">' .get_the_excerpt().'</span></a></li>';
            endwhile;
            wp_reset_postdata();
            ?>
Viewing 1 replies (of 1 total)
  • The only thing I can think of is that there’s a pre_get_posts somewhere in a theme or plugin that’s causing the issue.

    On a sidenote, I wouldn’t save the whole object in the transient, I feel like that could cause problems ( maybe even the core issue here ). It would be better / easier to save the results of the query instead of the whole query itself. For example:

    global $post;
    
    if( false === ( $commentary_posts = get_transient( 'commentary_results' ) ) ) {
    
    	$commentary = new WP_Query( array(
    		'post_type' 	 => 'post',
    		'post_status' 	 => 'publish',
    		'posts_per_page' => 4,
    		'no_found_rows'  => true,
    		'cat' 			 => 29,
    	) );
    	
    	if( $commentary->have_posts() ) {
    	
    		// Array of post objects
    		$commentary_posts = $commentary->posts
    		set_transient( 'commentary_results', $commentary_posts, 60*60*4 );
    	}
    	
    }
    
    if( $commentary_posts ) {
    
    	foreach( $commentary_posts as $post ) {
    		
    		// Setup post object functions: the_*() get_the_*()
    		setup_postdata( $post );
    		
    		printf(
    			'<li><a href="%1$s"><h4>%2$s</h4><span class="excerpt">%3$s</span></a></li>',
    			get_permalink(),
    			get_the_title(),
    			get_the_excerpt()
    		);
    	
    	}
    	
    	// Reset global $poset
    	wp_reset_postdata();
    	
    }
Viewing 1 replies (of 1 total)
  • The topic ‘WordPress posts_per_page being ignored?’ is closed to new replies.