\Featured_Content

Featured Content.

This module will allow users to define a subset of posts to be displayed in a theme-designated featured content area.

This feature will only be activated for themes that declare that they support it. This can be done by adding code similar to the following during the "after_setup_theme" action:

add_theme_support( 'featured-content', array(

'filter'     => 'mytheme_get_featured_content',
'max_posts'  => 20,
'post_types' => array( 'post', 'page' ),

) );

For maximum compatibility with different methods of posting users will designate a featured post tag to associate posts with. Since this tag now has special meaning beyond that of a normal tags, users will have the ability to hide it from the front-end of their site.

Summary

Methods
Properties
Constants
setup()
init()
wp_loaded()
get_featured_posts()
get_featured_post_ids()
delete_transient()
pre_get_posts()
delete_post_tag()
hide_featured_term()
hide_the_featured_term()
register_setting()
customize_register()
enqueue_scripts()
render_form()
get_setting()
validate_settings()
switch_theme()
$max_posts
$post_types
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$max_posts

$max_posts

The maximum number of posts that a Featured Content area can contain. We define a default value here but themes can override this by defining a "max_posts" entry in the second parameter passed in the call to add_theme_support( 'featured-content' ).

$post_types

$post_types

The registered post types supported by Featured Content. Themes can add Featured Content support for registered post types by defining a 'post_types' argument (string|array) in the call to add_theme_support( 'featured-content' ).

Methods

setup()

setup()

Instantiate.

All custom functionality will be hooked into the "init" action.

init()

init()

Conditionally hook into WordPress.

Themes must declare that they support this module by adding add_theme_support( 'featured-content' ); during after_setup_theme.

If no theme support is found there is no need to hook into WordPress. We'll just return early instead.

wp_loaded()

wp_loaded()

Hide "featured" tag from the front-end.

Has to run on wp_loaded so that the preview filters of the customizer have a chance to alter the value.

get_featured_posts()

get_featured_posts() : array

Get featured posts

This method is not intended to be called directly. Theme developers should place a filter directly in their theme and then pass its name as a value of the "filter" key in the array passed as the $args parameter during the call to: add_theme_support( 'featured-content', $args ).

Returns

array

get_featured_post_ids()

get_featured_post_ids() : array

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.

Returns

array —

Array of post IDs.

delete_transient()

delete_transient()

Delete Transient.

Hooks in the "save_post" action.

pre_get_posts()

pre_get_posts(\WP_Query $query) : \WP_Query

Exclude featured posts from the blog query when the blog is the front-page.

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.

Parameters

\WP_Query $query

Returns

\WP_Query —

Possibly modified WP_Query

delete_post_tag()

delete_post_tag(int $tag_id) : void

Reset tag option when the saved tag is deleted.

It's important to mention that the transient needs to be deleted, too. While it may not be obvious by looking at the function alone, the transient is deleted by Featured_Content::validate_settings().

Hooks in the "delete_post_tag" action.

Parameters

int $tag_id

The term_id of the tag that has been deleted.

hide_featured_term()

hide_featured_term(array $terms, array $taxonomies) : array

Hide featured tag from displaying when global terms are queried from the front-end.

Hooks into the "get_terms" filter.

Parameters

array $terms

A list of term objects. This is the return value of get_terms().

array $taxonomies

An array of taxonomy slugs.

Returns

array —

$terms

hide_the_featured_term()

hide_the_featured_term(array $terms, int $id, array $taxonomy) : array

Hide featured tag from displaying when terms associated with a post object are queried from the front-end.

Hooks into the "get_the_terms" filter.

Parameters

array $terms

A list of term objects. This is the return value of get_the_terms().

int $id

The ID field for the post object that terms are associated with.

array $taxonomy

An array of taxonomy slugs.

Returns

array —

$terms

register_setting()

register_setting() : void

Register custom setting on the Settings -> Reading screen.

customize_register()

customize_register(\WP_Customize_Manager $wp_customize)

Add settings to the Customizer.

Parameters

\WP_Customize_Manager $wp_customize

Theme Customizer object.

enqueue_scripts()

enqueue_scripts()

Enqueue the tag suggestion script.

render_form()

render_form()

Renders all form fields on the Settings -> Reading screen.

get_setting()

get_setting(string $key) : mixed

Get settings

Get all settings recognized by this module. This function will return all settings whether or not they have been stored in the database yet. This ensures that all keys are available at all times.

In the event that you only require one setting, you may pass its name as the first parameter to the function and only that value will be returned.

Parameters

string $key

The key of a recognized setting.

Returns

mixed —

Array of all settings by default. A single value if passed as first parameter.

validate_settings()

validate_settings(array $input) : array

Validate settings

Make sure that all user supplied content is in an expected format before saving to the database. This function will also delete the transient set in Featured_Content::get_featured_content().

Parameters

array $input

Returns

array —

$output

switch_theme()

switch_theme() : void

Removes the quantity setting from the options array.