All sticky posts are returned in custom query

This is default behavior when the post__in parameter is used. If you read the docs, you will see that you should set 'ignore_sticky_posts' => 1 to eliminate sticky posts being queried

EDIT

Your query arguments should look like this

$recommended_args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'post__in' => get_option('sticky_posts'),
    'orderby' => 'date',
    'ignore_sticky_posts' => 1
);

EDIT 2

By default, WP_Query (no matter if this is the main query or a custom query) searches and returns all sticky posts at the top of the first page. All other query parameters are ignored, specially posts_per_page. If you have 100 stickies, and posts_per_page is set to 3, all 100 sticky posts will be returned regardless with 3 posts that is not in the sticky posts array

These sticky posts is then removed from its original place in the query to avoid duplicates. When you pass get_option( 'sticky_posts' ) to post__in, these sticky posts are simply removed from the query as WP_Query has already returned them as sticky posts, this is to avoid them being shown as duplicates.

To make your query work, you have to “switch” off the sticky posts feature so that WP_Query can exclude them and just focus on the query at hand. For this you will need to set the ignore_sticky_posts paramter to 1 which means that sticky posts will be ignored

I hope this makes sense now

Leave a Comment