1

I'm working with Custom Query in WordPress, Basically, I'm showing 4 most recent posts of a category having ID 4 and my query is as follows:-

$args = array(
  'post_type' => 'post' ,
  'orderby' => 'date' ,
  'order' => 'DESC' ,
  'posts_per_page' => 4,
  'cat'         => '3',
  'paged' => get_query_var('paged'),
  
); 
$q = new WP_Query($args);

This is working fine, but here I have an additional requirement. I want to add sticky posts as well i.e Posts will be stick to the top no matters these posts are recent or old, and total posts_per_page should be always 4 including sticky and recent posts.

e.g If there is no sticky post then I'll show 4 most recent posts and no sticky post. But if there is 1 sticky post then there will be 1 sticky post and 3 most recent posts, a total of 4 posts.

What modification should I made in my Query? Thank you.

  • A question, Are the sticky posts in the same category? The query should work as you wanted without any further modifications. – Hector Oct 16 at 5:01
  • yes in same category – Ali Bhutta Oct 16 at 6:00
  • will this work without any modification? even for sticky posts? – Ali Bhutta Oct 16 at 6:01
  • Yes Just test it, Sticky posts will be shown on top of other posts by default. – Hector Oct 16 at 6:02
  • thanks for your precious time and experience – Ali Bhutta Oct 16 at 6:10
1
$sticky = get_option( 'sticky_posts' );

$args = array(
  'post_type'           => 'post' ,
  'orderby'             => 'date' ,
  'order'               => 'DESC' ,
  'posts_per_page'      => 4,
  'cat'                 => '3',
  'paged'               => get_query_var('paged'),
  'post__in'            => isset( $sticky[0] ) ? $sticky[0] : array(),
  'ignore_sticky_posts' => 1,
  
); 
$q = new WP_Query($args);

The above code will include only the first sticky post and if it does not exist, it will get normal posts.

If you want to show all of the existing sticky posts, then use the following code:

$args = array(
  'post_type'           => 'post' ,
  'orderby'             => 'date' ,
  'order'               => 'DESC' ,
  'posts_per_page'      => 4,
  'cat'                 => '3',
  'paged'               => get_query_var('paged'),
  'ignore_sticky_posts' => false,
  
); 
$q = new WP_Query($args);
| improve this answer | |
  • but there is no limitation for sticky posts, it may have 1 or > 1 sticky posts – Ali Bhutta Oct 13 at 5:47
  • Edited to include detailed answer. – Hector Oct 13 at 6:08
  • Thank you for your effort and help, your first query is returning $q->post_count:0 and the last/2nd query is giving the same result as my above-given query while I have 2 sticky posts – Ali Bhutta Oct 13 at 6:14
  • in case I replace this line in your first query 'post__in'=> $sticky ? $sticky : [], it returns only sticky posts – Ali Bhutta Oct 13 at 6:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.