How do I add a custom post type to the Featured Content in twenty fourteen theme?

The twentyfourteen_get_featured_posts filter:

It took some digging, to figure out how the twentyfourteen_get_featured_posts filter is used in the TwentyFourteen theme 😉


The featured content is fetched with:

$featured_posts = twentyfourteen_get_featured_posts();

but this function is only this single line:

return apply_filters( 'twentyfourteen_get_featured_posts', array() );

so where’s the meat?


We find it in the Featured_Content class, starting with this line:

add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );

where $filter comes from:

$filter = $theme_support[0]['featured_content_filter'];

where:

$theme_support = get_theme_support( 'featured-content' );

In functions.php we find:

// Add support for featured content.
add_theme_support( 'featured-content', array(
    'featured_content_filter' => 'twentyfourteen_get_featured_posts',
    'max_posts' => 6,
) );

so we finally see that:

$filter === 'twentyfourteen_get_featured_posts';

Example:

To override the default featured content posts, you can then try this:

add_filter( 'twentyfourteen_get_featured_posts', function( $posts ){

    // Modify this to your needs:
    $posts = get_posts( array(
        'post_type'       => array( 'cpt1', 'cpt2' ),
        'posts_per_page'  => 6,
        'featured_tax'    => 'featured_term' 
    ) );

    return $posts;

}, PHP_INT_MAX );

The next step would be to connect it to the theme customizer and maybe cache it.

Hopefully you can continue the journey from here 😉

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)