Last active
August 29, 2015 14:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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