• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Featured_Content::pre_get_posts() – Exclude featured posts from the home page blog query.

You appear to be a bot. Output may be restricted

Description

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.

Usage

$WP_Query = Featured_Content::pre_get_posts( $query );

Parameters

$query
( WP_Query ) required – WP_Query object.

Returns

WP_Query Possibly-modified WP_Query.

Source

File name: twentyfourteen/inc/featured-content.php


Lines:

1 to 30 of 30
  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 exclude list.
    $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 );
  }
 

 View on Trac

Published: 19th December 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: Featured_Content::pre_get_posts
Class ref: Featured_Content
Plugin ref: Twenty Fourteen
Version: 3.0
Sourcefile: inc/featured-content.php
File ref: inc/featured-content.php
Deprecated?: No
API Letters: C,F,G,P

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 5.6. PHP: 7.3.26
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites