Display All Sticky Post Before Regular Post

You can use a custom loop to get all stick post and then return all normal post. You may have to modify this code a bit but here is how you can return all sticky post. You can play with the second custom loop for the individual pages and return just the one post.

//is sticky 
$sticky = get_option( 'sticky_posts' );
if(!empty($sticky)){
 $args = array(
    'posts_per_page' => -1, //get all post
    'post__in'  => $sticky, //are they sticky post
 );

 // The Query
 $the_query = new WP_Query( $args );

 // The Loop //we are only getting a list of the title as a li see the loop docs for details on the loop or copy this from index.php (or posts.php)
 while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
 }
 wp_reset_query(); //reset the original WP_Query
}

//now get the not sticky post
$args2 = array(
    'posts_per_page' => -1, //get all post
    'post__not_in'  => $sticky, //are they NOT sticky post
);
// The Query
$the_query2 = new WP_Query( $args2 );

// The Loop....

This is a bit of starter code as I’m not going to write your project for you and it’s hard to figure out exactly what you’re trying to do without code/visual example.

Leave a Comment