Support » Fixing WordPress » Sticky posts do not work with category__in, possible solution?

  • Sticky posts do not bubble to the top of the loop like they should as mentioned in the codex if combined with category__in.

    Example of the issue dating back two years:

    My current query which doesn’t return sticky posts to the top:

    query_posts( array(
        'post_type' => array('post', 'careers-post'),
        'paged' => $paged,
        'posts_per_page' => 6,
        'category__in' => $page_categories,
        'ignore_sticky_posts' => 0
    ) );

    I need to maintain pagination and posts per page consistent which is why I havnt opted for the double query/loop approach. Any ideas on how I can resolve this?

    Any help is greatly appreciated
    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer



    On what theme template file do you need this?

    This is in a custom template file in a custom theme. Not too dissimilar to the generic template files of most themes, header, loop, footer, etc.

    It’s for the main loop on the page. We have a temporary fix in place which uses regex to change the database query directly in order to get it to grab sticky posts first.

    Moderator keesiemeijer



    Stickies only display on your home page. Do you have a static front Page? Or are you using index.php to show the posts on your home page?

    Moderator keesiemeijer



    Well I’m guessing your query is in index.php because sticky posts only show up on the home page.

    I’ve tried this before with this:

    But for your query I would use multiple loops in your index.php.
    example index.php:

    You’ll need to put this in your theme’s functions.php for this to work:

    Set how many posts you want to show per page in the functions code:

    // set posts per page here
    $posts_per_page = 6;

    Change the query to what you need in index.php:

    $args_loop = array(
      'post_type' => array('post', 'careers-post'),
      'category__in' => $page_categories,
      'posts_per_page' => $posts_per_page, // required
      'paged' => $paged // required

    In this example all pages display 6 posts. If you have more than 6 sticky posts they will be transferred to the next page. You can also limit the number of sticky posts you want to show by changing this in functions.php:

    // limit sticky posts (# number or false)
    $limit_sticky_posts = false;

    I’ve used some function from theme Twenty Twelve as an example in index.php. Replace all instances of this with your own pagination functions if you’re not using Twenty Twelve:

    <?php twentytwelve_content_nav( 'nav-below' ); ?>

    And replace this with your own loop code:

    <?php get_template_part( 'content', get_post_format() ); ?>

    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Thank you for the ideas I’ll have a go at implementing them in a bit.

    It is a static page (not index.php, but xxx-template.php). And it does work if I remove the ‘category__in’ which is why I thought it might be a bug. Do you know why sticky posts are only supposed to work on the homepage? just wondering about the logic behind putting so much importance on the home/blog page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sticky posts do not work with category__in, possible solution?’ is closed to new replies.