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.
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.
Share Improve this question asked Oct 13, 2020 at 5:18 Ali BhuttaAli Bhutta 1315 bronze badges 5- A question, Are the sticky posts in the same category? The query should work as you wanted without any further modifications. – Hector Commented Oct 16, 2020 at 5:01
- yes in same category – Ali Bhutta Commented Oct 16, 2020 at 6:00
- will this work without any modification? even for sticky posts? – Ali Bhutta Commented Oct 16, 2020 at 6:01
- Yes Just test it, Sticky posts will be shown on top of other posts by default. – Hector Commented Oct 16, 2020 at 6:02
- thanks for your precious time and experience – Ali Bhutta Commented Oct 16, 2020 at 6:10
1 Answer
Reset to default 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);