Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3372656
changes from patch
adamsilverstein Sep 9, 2020
713b4bb
Merge remote-tracking branch 'origin/trunk' into ticket/19958
Jan 20, 2026
fc4fcce
Enable custom post types on the homepage selection, add 'Books' CPT.
Jan 23, 2026
593c421
Allow filtering post types displayed on the homepage and ensure prope…
Jan 26, 2026
967dda4
Merge branch 'WordPress:trunk' into ticket/19958
pbearne Jan 26, 2026
e8c263f
Merge branch 'ticket/19958' of https://github.com/pbearne/wordpress-d…
Jan 26, 2026
692cbe4
Update src/wp-admin/options-reading.php
pbearne Jan 28, 2026
3cef0e1
Update src/wp-admin/options-reading.php
pbearne Jan 28, 2026
b6bfb35
Update src/wp-admin/options-reading.php
pbearne Jan 28, 2026
71b25bd
Allow filtering of post types shown on the homepage archive list and …
Jan 28, 2026
c5208b4
Update 'show_in_home_page_list' defaults for post and page types
Jan 28, 2026
e7ae7b2
Merge branch 'trunk' into ticket/19958
Jan 29, 2026
dbc3b1c
Add PHPUnit tests for custom post type behavior on the homepage
Jan 29, 2026
c5aa4f2
Update PHPUnit annotations for `Tests_Query_HomePageCpt` to include `…
Jan 29, 2026
aaa778c
Merge remote-tracking branch 'upstream/trunk' into ticket/19958-unit-…
faisalahammad Nov 22, 2026
f642859
Tests: Add coverage for custom post types on the home page.
faisalahammad Nov 22, 2026
3b84a6e
Posts, Post Types: Fix PHPStan errors for home page post types.
faisalahammad Nov 22, 2026
6161532
Posts, Post Types: Store the home page post type in its own option.
faisalahammad Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/wp-admin/includes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ function populate_options( array $options = array() ) {

// 7.2.0
'wp_random_base' => '',

// 7.3.0
'show_archive_on_front' => 'post',
);

// 3.3.0
Expand Down
18 changes: 18 additions & 0 deletions src/wp-admin/options-reading.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@
</fieldset></td>
</tr>
<?php endif; ?>
<?php
$home_page_post_types = get_home_page_post_types();

// The setting is only needed when there is a choice other than posts.
if ( array_diff( $home_page_post_types, array( 'post' ) ) ) :
?>
<tr>
<th scope="row"><label for="show_archive_on_front"><?php _e( 'Homepage post type' ); ?></label></th>
<td>
<select name="show_archive_on_front" id="show_archive_on_front" aria-describedby="show-archive-on-front-description">
<?php foreach ( $home_page_post_types as $home_page_post_type ) : ?>
<option value="<?php echo esc_attr( $home_page_post_type ); ?>" <?php selected( $home_page_post_type, get_option( 'show_archive_on_front', 'post' ) ); ?>><?php echo esc_html( get_post_type_object( $home_page_post_type )->labels->name ); ?></option>
<?php endforeach; ?>
</select>
<p class="description" id="show-archive-on-front-description"><?php _e( 'Used when your homepage displays your latest posts.' ); ?></p>
</td>
</tr>
<?php endif; ?>
<tr>
<th scope="row"><label for="posts_per_page"><?php _e( 'Number of recent posts shown on archive pages' ); ?></label></th>
<td>
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
'show_on_front',
'page_on_front',
'page_for_posts',
'show_archive_on_front',
'blog_public',
),
'writing' => array(
Expand Down
11 changes: 11 additions & 0 deletions src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ final class WP_Post_Type {
*/
public $randomable = null;

/**
* Whether this post type can be shown on the home page in place of posts.
*
* Default false.
*
* @since 7.3.0
* @var bool $show_in_home_page_list
*/
public $show_in_home_page_list = false;

/**
* Whether to generate and allow a UI for managing this post type in the admin.
*
Expand Down Expand Up @@ -560,6 +570,7 @@ public function set_props( $args ) {
'publicly_queryable' => null,
'embeddable' => null,
'randomable' => null,
'show_in_home_page_list' => false,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
Expand Down
10 changes: 10 additions & 0 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,16 @@ public function parse_query( $query = '' ) {
$this->is_home = true;
}

// Show the latest posts of the chosen post type on the home page. Secondary queries are not changed.
if ( $this->is_home && $this->is_main_query() && empty( $query_vars['post_type'] ) && 'posts' === get_option( 'show_on_front' ) ) {
$home_post_type = get_option( 'show_archive_on_front', 'post' );

// Posts are the default, so the query var is only needed for other post types.
if ( 'post' !== $home_post_type && in_array( $home_post_type, get_home_page_post_types(), true ) ) {
$query_vars['post_type'] = $home_post_type;
}
}

// Correct `is_*` for 'page_on_front' and 'page_for_posts'.
if ( $this->is_home && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) {
$_query = wp_parse_args( $this->query );
Expand Down
7 changes: 7 additions & 0 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4966,6 +4966,7 @@ function wp_make_link_relative( $link ) {
*
* @since 2.0.5
* @since 7.1.5 The `show_on_front` option is sanitized.
* @since 7.3.0 The `show_archive_on_front` option is sanitized.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
Expand Down Expand Up @@ -5213,6 +5214,12 @@ function sanitize_option( $option, $value ) {
}
break;

case 'show_archive_on_front':
if ( ! in_array( $value, get_home_page_post_types(), true ) ) {
$value = 'post';
}
break;

case 'default_role':
if ( ! get_role( $value ) && get_role( 'subscriber' ) ) {
$value = 'subscriber';
Expand Down
67 changes: 49 additions & 18 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ function create_initial_post_types() {
register_post_type(
'post',
array(
'labels' => array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
),
'public' => true,
'randomable' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array(
'public' => true,
'randomable' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array(
'title',
'editor' => array( 'notes' => true ),
'author',
Expand All @@ -50,10 +50,11 @@ function create_initial_post_types() {
'revisions',
'post-formats',
),
'show_in_rest' => true,
'show_in_abilities' => true,
'rest_base' => 'posts',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'show_in_rest' => true,
'show_in_abilities' => true,
'rest_base' => 'posts',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'show_in_home_page_list' => true,
)
);

Expand Down Expand Up @@ -1681,6 +1682,33 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
return wp_filter_object_list( $wp_post_types, $args, $operator, $field );
}

/**
* Retrieves the post types whose latest posts can be shown on the home page.
*
* @since 7.3.0
*
* @return string[] Array of post type names.
*/
function get_home_page_post_types() {
$post_types = array_values( get_post_types( array( 'show_in_home_page_list' => true ) ) );

/**
* Filters the post types whose latest posts can be shown on the home page.
*
* Defaults to the post types registered with `show_in_home_page_list` set to true.
* Developers can use this filter to adjust the post types offered on the
* Settings > Reading screen.
*
* @since 7.3.0
*
* @param string[] $post_types Array of post type names.
*/
$post_types = apply_filters( 'post_types_allowed_on_home_page', $post_types );

// Pages are excluded because they are set as the home page through the static page setting.
return array_values( array_diff( array_filter( $post_types, 'post_type_exists' ), array( 'page' ) ) );
}

/**
* Registers a post type.
*
Expand All @@ -1706,6 +1734,7 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature.
* @since 5.9.0 The `rest_namespace` argument was added.
* @since 7.2.0 The `show_in_abilities` argument was added.
* @since 7.3.0 The `show_in_home_page_list` argument was added.
*
* @global array $wp_post_types List of post types.
*
Expand Down Expand Up @@ -1740,6 +1769,8 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* @type bool $randomable Whether posts of this post type can be selected by random content
* redirects, such as example.com/random/. If not set, the default is
* true when $publicly_queryable is true and $has_archive is set.
* @type bool $show_in_home_page_list Whether the latest posts of this post type can be shown on the home
* page in place of posts. Not supported for pages. Default false.
* @type bool $show_ui Whether to generate and allow a UI for managing this post type in the
* admin. Default is value of $public.
* @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui
Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/tests/post/resetFrontPageSettingsForPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Tests for _reset_front_page_settings_for_post().
*
* @group post
* @ticket 19958
*
* @covers ::_reset_front_page_settings_for_post
*/
class Tests_Post_ResetFrontPageSettingsForPost extends WP_UnitTestCase {

public function test_trashing_front_page_resets_front_page_settings() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $page_id );

wp_trash_post( $page_id );

$this->assertSame( 'posts', get_option( 'show_on_front' ), 'show_on_front should fall back to posts.' );
$this->assertSame( 0, (int) get_option( 'page_on_front' ), 'page_on_front should be cleared.' );
}

public function test_deleting_posts_page_resets_posts_page_setting() {
$front_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$posts_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $front_id );
update_option( 'page_for_posts', $posts_id );

wp_delete_post( $posts_id, true );

$this->assertSame( 0, (int) get_option( 'page_for_posts' ), 'page_for_posts should be cleared.' );
$this->assertSame( 'page', get_option( 'show_on_front' ), 'show_on_front should not change.' );
$this->assertSame( $front_id, (int) get_option( 'page_on_front' ), 'page_on_front should not change.' );
}

public function test_trashing_other_page_keeps_front_page_settings() {
$front_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$other_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $front_id );

wp_trash_post( $other_id );

$this->assertSame( 'page', get_option( 'show_on_front' ), 'show_on_front should not change.' );
$this->assertSame( $front_id, (int) get_option( 'page_on_front' ), 'page_on_front should not change.' );
}

public function test_trashing_sticky_post_unsticks_it() {
$post_id = self::factory()->post->create();
stick_post( $post_id );

wp_trash_post( $post_id );

$this->assertFalse( is_sticky( $post_id ) );
}
}
Loading
Loading