WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Order posts by "sticky posts" first? (18 posts)

  1. km
    Member
    Posted 4 years ago #

    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

  2. vtxyzzy
    Member
    Posted 4 years ago #

    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?

  3. km
    Member
    Posted 4 years ago #

    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...
  4. vtxyzzy
    Member
    Posted 4 years ago #

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

  5. km
    Member
    Posted 4 years ago #

    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

  6. km
    Member
    Posted 4 years ago #

    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

  7. vtxyzzy
    Member
    Posted 4 years ago #

    Give this a try:

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

  8. km
    Member
    Posted 4 years ago #

    Awesome, that works a treat! Thanks!

  9. vtxyzzy
    Member
    Posted 4 years ago #

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

  10. km
    Member
    Posted 4 years ago #

    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

  11. vtxyzzy
    Member
    Posted 4 years ago #

    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...
  12. km
    Member
    Posted 4 years ago #

    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

  13. keesiemeijer
    moderator
    Posted 4 years ago #

    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; ?>
  14. vtxyzzy
    Member
    Posted 4 years ago #

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

  15. keesiemeijer
    moderator
    Posted 4 years ago #

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

  16. vtxyzzy
    Member
    Posted 4 years ago #

    Of course you are right keesiemeijer!

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

  17. km
    Member
    Posted 4 years ago #

    Ah, thanks. No luck with that plugin I'm afraid.

    So multiple loops... as in returning just the sticky posts, and then returning the non-sticky posts - in 2 separate loops?

    Cheers,
    Kim

  18. raxit4u2
    Member
    Posted 3 years ago #

    Hi,

    &caller_get_posts=0 shows sticky post at top of page but it shows all posts on index page with out pagination. Pagination works only on normal posts.

    Anyone have idea to show sticky post on top with pagination ? For e.g. if i have total 100 posts and 30 of them are sticky. Post per page sets to 10 than for first 3 pages, it should show sticky posts and than after normal post of rest of 7 pages.

    Any help ?

    Thanks,
    Rakshit Patel.
    PHP Programmer India

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.