Featured_Content::pre_get_posts PHP Method

pre_get_posts() public static method

Filter the home page posts, and remove any featured post ID's from it. Hooked onto the 'pre_get_posts' action, this changes the parameters of the query before it gets any posts.
public static pre_get_posts ( WP_Query $query ) : WP_Query
$query WP_Query
return WP_Query Possibly modified WP_Query
        public static function pre_get_posts($query)
        {
            // Bail if not home or not main query.
            if (!$query->is_home() || !$query->is_main_query()) {
                return;
            }
            // Bail if the blog page is not the front page.
            if ('posts' !== get_option('show_on_front')) {
                return;
            }
            $featured = self::get_featured_post_ids();
            // Bail if no featured posts.
            if (!$featured) {
                return;
            }
            $settings = self::get_setting();
            // Bail if the user wants featured posts always displayed.
            if (true == $settings['show-all']) {
                return;
            }
            // We need to respect post ids already in the blacklist.
            $post__not_in = $query->get('post__not_in');
            if (!empty($post__not_in)) {
                $featured = array_merge((array) $post__not_in, $featured);
                $featured = array_unique($featured);
            }
            $query->set('post__not_in', $featured);
        }