$post_ids, 'posts_per_page' => count( $post_ids ), ) ); return $featured_posts; } /** * Get featured post IDs * * This function will return the an array containing the * post IDs of all featured posts. * * Sets the "featured_content_ids" transient. * * @static * @access public * * @return array Array of post IDs. */ public static function get_featured_post_ids() { // Get array of cached results if they exist. $featured_ids = get_transient( 'featured_content_ids' ); if ( false === $featured_ids ) { // Query for featured posts. $featured_ids = get_posts( array( 'fields' => 'ids', 'numberposts' => self::$max_posts, 'suppress_filters' => false, 'meta_key' => 'visibility_settings_select', 'meta_value' => 'featured' ) ); set_transient( 'featured_content_ids', $featured_ids ); } // Ensure correct format before return. return array_map( 'absint', $featured_ids ); } /** * Delete featured content ids transient. * * Hooks in the "save_post" and "switch_theme" action. * * @static * @access public */ public static function delete_transient() { delete_transient( 'featured_content_ids' ); } /** * Exclude featured posts from the home page blog query. * * 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. * * @static * @access public * * @param WP_Query $query WP_Query object. * @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; } // 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 ); } } // Featured_Content Featured_Content::setup();