Skip to content

Instantly share code, notes, and snippets.

@micahwave
Created January 12, 2012 21:33
Show Gist options
  • Save micahwave/1603272 to your computer and use it in GitHub Desktop.
Save micahwave/1603272 to your computer and use it in GitHub Desktop.
exlude home posts
/**
* Filter out any lede post ids or hidden posts from the main query
*/
function time_exclude_home_posts( $query ) {
// make sure were on the homepage 1st
if( ( is_home() || is_front_page() ) && $query->is_main_query() ) {
$hidden_post_ids = array();
// check to see if there are any lede ids
$lede_post_ids = time_get_lede_post_ids();
$hidden_posts = wp_cache_get( 'time_hidden_posts', TIME_CACHE );
if( !$hidden_posts ) {
$hidden_posts = get_posts(
array(
'post_status' => 'publish',
'posts_per_page' => 50,
'meta_key' => 'time_hide_on_home',
'meta_value' => 1
)
);
if( $hidden_posts ) {
wp_cache_set( 'time_hidden_posts', $hidden_posts, TIME_CACHE, 3600 );
}
}
// lets just get the post ids
if( $hidden_posts ) {
$hidden_post_ids = wp_list_pluck( $hidden_posts, 'ID' );
}
// merge our hidden posts with any lede post ids
$hidden_post_ids = array_merge( $hidden_post_ids, $lede_post_ids );
if( count( $hidden_post_ids ) ) {
$query->query_vars['post__not_in'] = $hidden_post_ids;
}
}
}
add_action( 'pre_get_posts', 'time_exclude_home_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment