WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sticky posts not "sticking" when using query_posts() (3 posts)

  1. DaveE
    Member
    Posted 4 years ago #

    Hello,

    Maybe I'm completely misunderstanding something about the sticky posts functionality, but for the life of me I can't get sticky posts to float up/stick to the top when I query posts by category before the loop. I've looked through all the sticky post documentation/posts and don't understand what I'm doing wrong.

    For example, in my blog homepage (index.php) I've added query_posts('category_name=news'); just before the main loop. I have a post mapped to the news category and also set to sticky. When viewing the site homepage the post doesn't stick to the top; instead it shows up in the proper chronological order further down the page. If I remove the query_posts call, the post sticks correctly, but the loop also shows a bunch of non "news" posts that I don't want to display.

    My goal for the blog homepage is to only show posts from the News category and make sure any sticky posts in the News category are stuck to the top of the homepage (as well as the related category page).

    Am try to do something that isn't possible in one loop? Is there a better way to accomplish my goal?

    Thanks for the help!

    DaveE

  2. kz
    Member
    Posted 4 years ago #

    $sticky = get_option('sticky_posts');
    query_posts(array(
      'post__in' => $sticky,
      'category_name' => 'news'
    ));
    
    /* The loop for sticky here. */
    
    query_posts(array(
      'post__not_in' => $sticky;
      'category_name' => 'news'
    ));
    
    /* The loop for news category without sticky here. */
    
    wp_reset_query();
  3. DaveE
    Member
    Posted 4 years ago #

    Thanks for the feedback. Your suggestion worked perfectly. Just out of curiosity, why does using the query_posts() object kill the "standard" method of displaying sticky posts at the top? Wouldn't the sticky posts option/functionality be part of the default query_posts() query?

    Thanks again,

    DaveE

Topic Closed

This topic has been closed to new replies.

About this Topic