8

We all know that ignore_sticky_posts is used to exclude sticky post from your custom query.

But why on codex page https://codex.wordpress.org/Sticky_Posts, they use ignore_sticky_posts in querying sticky posts??

$args = array(
'posts_per_page' => 1,
'post__in'  => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );

This is so confusing and doesn't make any sense, you want it and yet you exclude it?

13

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 to true or 1, 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):

  1. 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.

  2. 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.

  • 2
    WordPress should include your answer on their sticky post page! – 吉 宁 Mar 22 '17 at 8:20
  • 1
    I'm glad it helped you. WordPress codex is editable, even you can edit it and add this answer as reference for others ;) – Fayaz Mar 22 '17 at 9:23
  • 2
    This answer is very much underrated. I've actually come back to it twice now, a year apart. That's how confusing this parameter is. – Benji Mar 28 '18 at 12:28

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.