Support » Fixing WordPress » WP_Query args to match given number of latest posts, both sticky and non-sticky

  • Resolved ezeocca

    (@ezeocca)



    Hello,

    I use the following code to query a give number (5) of most recent posts to be displayed in my sidebar:

    <?php
    	$leaflet_args = array(
    	'showposts' => 5,	// display the most recent 5 posts
    );
    	$leaflet_posts = new WP_Query($leaflet_args);
    
    	if($leaflet_posts->have_posts()) :
    		while($leaflet_posts->have_posts()) :
    			$leaflet_posts->the_post();
    
    				get_template_part("content", "leaflet");
    
    		endwhile;
    	else: ?>
    		<div>Oops, there are no posts.</div>
    <?php
    endif;
    ?>

    Unfortunately, the ‘showposts’ number is just not working, since I have 4 posts marked as sticky, and they always show up in the query.
    Is there an argument option I can pass to WP_Query not to prioritize sticky posts but just simply query the 5 most recent ones?

    Thank you!

Viewing 1 replies (of 1 total)
  • Luckily, I was able to figure out the solution:

    You need to add 'ignore_sticky_posts' => 1 to your WP_Query $args.

Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query args to match given number of latest posts, both sticky and non-sticky’ is closed to new replies.