Skip to content

Instantly share code, notes, and snippets.

@rfvcorreia
Created September 26, 2013 13:42
Show Gist options
  • Select an option

  • Save rfvcorreia/6714394 to your computer and use it in GitHub Desktop.

Select an option

Save rfvcorreia/6714394 to your computer and use it in GitHub Desktop.
Get Sticky Posts, if not enough get latest posts
<?php
$sticky = get_option( 'sticky_posts' );
$total = 6;
$featured_args = array(
'posts_per_page' => $total,
'post__in' => $sticky,
'post_status' => 'publish',
'no_found_rows' => true
);
// The Featured Posts query.
$featured = new WP_Query( $featured_args );
while ( $featured->have_posts() ) : $featured->the_post();
//Post Content
endwhile;
if ( count( $featured->posts ) < $total ) {
//Get remaining non-sticky posts
$nonstickyTotal = $total - count( $featured->posts );
$remaining_args = array(
'posts_per_page' => $nonstickyTotal,
'post__not_in' => $sticky,
'post_status' => 'publish',
'no_found_rows' => true
);
// The Featured Posts query.
$featured = new WP_Query( $remaining_args );
while ( $featured->have_posts() ) : $featured->the_post();
//Post Content
endwhile;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment