Make Sticky Post the First Post In WordPress Loop


Make Sticky Post the First Post In WordPress Loop -

Hello Guys ! few days back a got a wordpress project ..where the default wordpress loop was not able to make sticky post on the top instead the sticky posts were in between the ordinary posts ,But the Requirement was to make sticky post on top of the wordpress loop.

So in-order to attain this functionality i researched a finally find our a conclusion that instead of running  query we need to run two queries ,One is for the sticky posts only and other for ordinary posts only , off course we’ll put sticky post on top of ordinary query.

So here is a quick fix how to make Sticky Post the first post in the WordPress Loop.

WordPress Sticky Post the First Post In Loop quick  Fix -

So as i suggested we gonna run two queries instead of just one .There are two ways we can do this .using Wp_query object or the alternate query_posts ,Im gonna Put down the code for both mehods below.Lets take a look

Sticky Post on top of the Loop -Using “Wp_Query” Loops -

Firstly we gonna use wp_query object and attain what we need.

 $sticky = get_option( 'sticky_posts' );
    $args_ordinary = array(
        'showposts'     => -1,
        'post__not_in' => $sticky
    );
    $args_sticky = array(
        'posts_per_page' => -1,
        'post__in'  => $sticky
    );

    $the_query_sticky = new WP_Query($args_sticky);
    $the_query_ordinary = new WP_Query($args_ordinary);

    if(!$the_query_sticky->have_posts() && !$the_query_ordinary->have_posts()){
        //echo '<h1>NO POSTS FOUND</h1>';
    }else{

    if ( $sticky[0] ) {
    while ($the_query_sticky->have_posts()) : $the_query_sticky->the_post();
      //sticky if so...
    endwhile;
    }

    while ($the_query_ordinary->have_posts()) : $the_query_ordinary->the_post();
        // non sticky
    endwhile;
}

These Loops Will List Sticky Post on the Top of the Ordinary Posts .

Sticky Post on top of the Loop -Using “query_posts” Loops -

Now we are gonna use the alternate loop which is query post.The result is same in the both.

<?php
$sticky = get_option( 'sticky_posts' );
 $args_ordinary = array(
        'showposts'     => -1,
        'post__not_in' => $sticky
    );
    $args_sticky = array(
        'posts_per_page' => -1,
        'post__in'  => $sticky
    );
query_posts($args_sticky);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
  //sticky post
<?php endwhile; ?>
<?php endif; ?>

// Now Ordinary Posts
query_posts($args_ordinary);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
  //ordinary post
<?php endwhile; ?>
<?php endif; ?>

These Are the Two methods for making Sticky posts come first on the Loop.But If you need pagination with them That gotta be problem with sticky post ,So there is a little fix for that So that Sticky posts won’t get repeated on the 2nd ,3rd and so on pages.we need to tell sticky post that if the page is first then it shows and else it wont , So that would simply be a if else statement.here it is.

The Pagination Fix for Sticky Posts -

To make Pagination work correctly and prevent sticky posts repeat on and 2nd and further pages you need to wrap the Sticky post loop inside following code-

$paged = get_query_var('paged');

if ($paged < 1 ) {
// Sticky Post Loop Here
}

I hope You liked this tiny post.I got in this situation so i think i better write the solution here.Tnx for visiting and reading this post.Stay healthy keep Visiting.

follow on pinterest button Make Sticky Post the First Post In Wordpress Loop


About the author

Ramandeep Singh Is Btech Graduate in Computer Science.He has interest in Programming.He knows .net ,vb.net,c#.Ramandeep Singh is certified web developer in PHP and PHP Framework(cakePHP).Ramandeep Likes to write Tutorials on Blog,that is why he founded DesignAeon

Leave a Reply