WordPress.org

Ready to get started?Download WordPress

Forums

Sticky posts do not work with category__in, possible solution? (6 posts)

  1. adamweclick
    Member
    Posted 1 year ago #

    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:
    http://wordpress.org/support/topic/category_in-ampamp-sticky

    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.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    On what theme template file do you need this?

  3. adamweclick
    Member
    Posted 1 year ago #

    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.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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?

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    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:
    http://wordpress.org/support/topic/wordpress-34-broke-my-paginaton-setup
    http://wordpress.org/support/topic/how-to-build-a-custom-query-in-twenty-eleven

    But for your query I would use multiple loops in your index.php.
    example index.php: http://pastebin.com/7rncMsGa

    You'll need to put this in your theme's functions.php for this to work: http://pastebin.com/15AzeFeA

    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() ); ?>

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

  6. adamweclick
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic