Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save tommcfarlin/0a9aa8a53ad962d8a1e2 to your computer and use it in GitHub Desktop.

Select an option

Save tommcfarlin/0a9aa8a53ad962d8a1e2 to your computer and use it in GitHub Desktop.
[WordPress] How to filter out posts that don't have a specific meta key before rendering them to the front-end.
<?php
add_action( 'pre_get_posts', array( $plugin_public, 'acme_exclude_this_post' ) );
/**
* Excludes all posts from the main feed that have the 'Exclude Post From Index' meta data set.
*
* @param object $query The query for retrieving the posts for the homepage.
* @return object $query
*/
public function acme_exclude_this_post( $query ) {
if ( $query->is_main_query() && $query->is_home() ) {
$query->set(
'meta_query',
array(
array(
'key' => 'exclude_post_from_index',
'compare' => 'NOT EXISTS'
)
)
);
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment