Wordpress - why ignore_sticky_posts in sticky post query
We all know that
ignore_sticky_posts
is used to exclude sticky post from your custom query.
- No, this assumption is wrong.
What ignore_sticky_posts
means:
Even though in natural English, ignore_sticky_posts
sounds like WordPress should ignore all sticky posts from the query, in reality that is not what WordPress does. Instead, you should read 'ignore_sticky_posts' => 1
argument as follows:
When
ignore_sticky_posts
is set totrue
or1
, WordPress will ignore the procedure of setting the sticky posts within your custom query.
What WordPress does when ignore_sticky_posts
is not set:
To clearly understand what 'ignore_sticky_posts' => 1
does, you need to understand what WordPress does when ignore_sticky_posts
argument is not set or it's set to false
or 0
(by default):
If there are posts within the query result that are part of stick posts, WordPress will push them to the top of the query result.
If any sticky post is not present within the query result, WordPress will get all those sticky posts from the database again and set them to the top of the query result.
So when the argument is set as 'ignore_sticky_posts' => 1
, WordPress simply ignores the above procedure, that's all. It doesn't exclude them specifically. For that you need to set post__not_in
argument.
Explanation of the codex example:
Now, let's come to the example from the codex:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
Here codex is only setting 'ignore_sticky_posts' => 1
to be efficient, nothing more. Even without having it, you will get the same expected result:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' )
);
$query = new WP_Query( $args );
However, in this case, since 'ignore_sticky_posts' => 1
argument is not set, WordPress will needlessly do all those procedure of setting sticky posts to the top of the results, even though all of these results (from this example) are only sticky posts.
Best way to learn something in WordPress is to examine the core CODE. So for even clearer understanding, examine this part of WordPress CODE.