Support » Fixing WordPress » Order posts by “sticky posts” first?

  • Resolved km

    (@kmaisch)


    Hi,
    Can anyone tell me how I would change this code:

    <?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post() ?>

    To display the “sticky” posts first? (in order of newest to oldest), followed by the non-sticky posts (also in order of newest to oldest)?

    Cheers,
    Kim

Viewing 15 replies - 1 through 15 (of 17 total)
  • The order of posts is determined by the arguments to query_posts. The order you describe is the default, so you should be getting that unless there is a query_posts call just before the code you posted, and it has arguments that change the order. Can you post about 10 – 15 lines of code ending in the ones you showed above?

    Thread Starter km

    (@kmaisch)

    Thanks vtxyzzy.

    It’s working OK for my index page… but just not in the Archive or Search pages.

    Query_posts doesnt seem to be used on these pages (for my theme anyway). This is what I have:

    <?php $category = get_the_category(); // get the active category information ?>
    <h2>Archive for <?php single_cat_title(); ?> (<?php echo $wp_query->found_posts ?>)</h2>
    <?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post() ?>
    etc...

    Are you using a free theme? If so, post the name and I will take a closer look.

    Thread Starter km

    (@kmaisch)

    Nah it’s a commercial theme…

    If I put <?php query_posts(”); ?> in front of the <?php if(have_posts()) : ?> line – it does then display the sticky posts first, however it then displays every post on the site… (on the archive and search pages)…

    I think the same applies to the default theme (it doesn’t have a query_posts function on the archive and search pages)…

    Cheers,
    Kim

    Thread Starter km

    (@kmaisch)

    Actually if I use <?php query_posts($query_string); ?> – that works better (but still doesn’t show the stickies first).

    I guess I need to add some additional options to query_posts to force it to display the stickies first?

    Kim

    Give this a try:

    <?php query_posts($query_string . '&caller_get_posts=0'); ?>

    Thread Starter km

    (@kmaisch)

    Awesome, that works a treat! Thanks!

    You are welcome! Now, please use the dropdown at top right to mark this topic ‘Resolved’.

    Thread Starter km

    (@kmaisch)

    Sorry, for some reason I thought this worked – but looking at it again, it still displays newer “non sticky” posts at the top of the search results and category pages (with the sticky posts at the bottom).
    Any other ideas?
    Kim

    Please print out the query_string and post it here.

    <?php $category = get_the_category(); // get the active category information ?>
    <h2>Archive for <?php single_cat_title(); ?> (<?php echo $wp_query->found_posts ?>)</h2>
    <h2>Query String:<?php echo $query_string; ?></h2>
    <?php if(have_posts()) : ?>
    <?php while(have_posts()) : the_post() ?>
    etc...
    Thread Starter km

    (@kmaisch)

    category_name=thenameofmycategory (on the archive page)
    Query String:s=searchwords (on the search page)

    Here is the relevant code on the search page:

    <?php query_posts($query_string . '&caller_get_posts=0'); ?>
    <?php if(have_posts()) : ?>
                    <?php $cat = get_query_var('cat'); ?>
    
                        <?php while(have_posts()) : the_post() ?>
    
                        <?php if (!in_category($cat) && $cat != 0) continue; ?>
    
                        <?php if (get_option('cp_search_ex_pages') == 'yes') { if (cp_is_type_page()) continue; } // exclude pages ?>
    
                        <?php if (get_option('cp_search_ex_blog') == 'yes') { if (in_category(CP_BLOG_CAT_ID) || cp_post_in_desc_cat(CP_BLOG_CAT_ID)) continue; } // exclude blog posts ?>
    
                       <?php if (is_sticky()){ ?>
    <div class="whiteblock feature_bg">
    <?php } else { ?>
    <div class="whiteblock can_chg_bg">
    <?php } ?>

    Not sure if that helps?
    Cheers,
    Kim

    Moderator keesiemeijer

    (@keesiemeijer)

    You could try a multiple loop. In the first only sticky posts and in the second only non sticky posts.
    like so:

    <?php
    if(is_search()){
    $key = 's';
    $current = $s;
    }
    if(is_category()) {
    $key = 'cat';
    $current = get_query_var('cat');
    }
    $sticky = new WP_Query(array('post__in'  => get_option('sticky_posts'), $key => $current));
    ?>
    <?php while ($sticky->have_posts()) : $sticky->the_post(); ?>
    <!-- (first loop) all stickey posts -->
    <?php endwhile; ?>
    
    <?php  $notStickey = new WP_Query(array('post__not_in'  => get_option('sticky_posts'), $key => $current)); ?>
    <?php while ($notStickey->have_posts()) : $notStickey->the_post(); ?>
    <!-- (second loop) all non sticky posts -->
    <?php endwhile; ?>

    I wonder why caller_get_posts=0 did not show in the query_string? Did you take it out before you ran the test?

    Moderator keesiemeijer

    (@keesiemeijer)

    I think sticky post only work on the front page and not in category.php or archive.php. Not sure.

    Of course you are right keesiemeijer!

    This plugin might help. If not, multiple loops are the answer.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Order posts by “sticky posts” first?’ is closed to new replies.