• nomen1

    (@nomen1)


    This is perplexing. I have a query loop set to display one post. No filters, and set to display newest first. So generally, this would end up being my most recent post. However, there will be times when I will have other posts that I want to stay in place, so I tested the sticky post feature to verify that it would work I expected, and that is when the things turned wonky.

    When setting an earlier published post to sticky, the resulting behavior is that the query loop shows TWO total posts. Oddly, within the editor, this behavior is not observed. Within the editor, only the most recent post is observed (i.e. the sticky post is exclude).

    I tried setting the query loop to max page = 1, but that did not change anything. Anybody know why this is happening or how to rectify this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @nomen1

    The behavior occurs because sticky posts are automatically included in WordPress queries, even when limiting the number of posts. To fix this, modify the query loop using pre_get_posts to exclude sticky posts explicitly:

    function exclude_sticky_posts($query) {
        if ($query->is_main_query() && !is_admin()) {
            $query->set('ignore_sticky_posts', 1);
        }
    }
    add_action('pre_get_posts', 'exclude_sticky_posts');
    

    This ensures only the specified number of posts is shown, ignoring sticky behavior.

    @nomen1

    Please provide more info about the WordPress version and the theme you are using. The default query loop behavior is to show stycky post on the top of every page, except the page where that post is originaly located. But, some themes, plugins, or page builder could change default behavior of the sticky posts feature.

    @dilip2615

    I think that using this function will exclude sticky posts from the query loop, so you must use two queries – one for sticky posts and one for all other posts.

    Thread Starter nomen1

    (@nomen1)

    I’m using the most recent version of WP, and I am just using a blank theme and building with the built in editor. Absolutely nothing fancy at all.

    This seems like a major bug. If you specify an exact number of posts for a query loop, there’s no justification for sticky posting to ignore that. If a user sets the post count = 1, then post count should = 1. If WordPress is allowing 1 = 2, that’s a very dangerous precedent as there is no state in nature where 1 = 2. The entire universe could implode.

    And really, it shouldn’t require manually modifying code to fix it. The whole point of using a platform like WordPress is to not have to deal with code to build a website. If a 20+ year piece of software can’t perform it’s own basic functions correctly without me having to go find wherever pre_get_posts happens to be and manually edit the code, then why does WordPress exist?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.