Tuesday, February 15, 2011

Wordpress Codex: Exclude Sticky Posts From Query

Exclude all sticky posts from the query:
query_posts( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );

Exclude sticky posts from a category. Return ALL posts within the category, but don't show sticky posts at the top. The 'sticky posts' will still show in their natural position (e.g. by date):
query_posts( 'ignore_sticky_posts=1&posts_per_page=3&cat=6' );



Exclude sticky posts from a category. Return posts within the category, but exclude sticky posts completely, and adhere to paging rules:
$paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
    'cat' => 3,
    'ignore_sticky_posts' => 1,
    'post__not_in' => $sticky,
    'paged' => $paged
);
query_posts( $args );

/* Get all Sticky Posts */
$sticky = get_option( 'sticky_posts' );

/* Sort Sticky Posts, newest at the top */
rsort( $sticky );

/* Get top 5 Sticky Posts */
$sticky = array_slice( $sticky, 0, 5 );

/* Query Sticky Posts */
query_posts( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
?>