diff --git a/src/wp-admin/includes/schema.php b/src/wp-admin/includes/schema.php
index 0c16cc2987b4f..9065b76a5889e 100644
--- a/src/wp-admin/includes/schema.php
+++ b/src/wp-admin/includes/schema.php
@@ -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
diff --git a/src/wp-admin/options-reading.php b/src/wp-admin/options-reading.php
index 8ea14a7178256..1f77ffce8ec56 100644
--- a/src/wp-admin/options-reading.php
+++ b/src/wp-admin/options-reading.php
@@ -176,6 +176,24 @@
+
+
|
diff --git a/src/wp-admin/options.php b/src/wp-admin/options.php
index 8f065f64473e6..a13914843ca9f 100644
--- a/src/wp-admin/options.php
+++ b/src/wp-admin/options.php
@@ -152,6 +152,7 @@
'show_on_front',
'page_on_front',
'page_for_posts',
+ 'show_archive_on_front',
'blog_public',
),
'writing' => array(
diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php
index 0767deeb34162..e1c2d5ae76522 100644
--- a/src/wp-includes/class-wp-post-type.php
+++ b/src/wp-includes/class-wp-post-type.php
@@ -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.
*
@@ -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,
diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php
index c5e048efe3806..0429a8e06ddf2 100644
--- a/src/wp-includes/class-wp-query.php
+++ b/src/wp-includes/class-wp-query.php
@@ -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 );
diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index ad12579fdafa1..feebbdb33cadb 100644
--- a/src/wp-includes/formatting.php
+++ b/src/wp-includes/formatting.php
@@ -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.
*
@@ -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';
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 3511fbfc81d7a..b50596950f41c 100644
--- a/src/wp-includes/post.php
+++ b/src/wp-includes/post.php
@@ -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',
@@ -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,
)
);
@@ -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.
*
@@ -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.
*
@@ -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
diff --git a/tests/phpunit/tests/post/resetFrontPageSettingsForPost.php b/tests/phpunit/tests/post/resetFrontPageSettingsForPost.php
new file mode 100644
index 0000000000000..ef38f1b8d0fe2
--- /dev/null
+++ b/tests/phpunit/tests/post/resetFrontPageSettingsForPost.php
@@ -0,0 +1,57 @@
+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 ) );
+ }
+}
diff --git a/tests/phpunit/tests/query/homePageCpt.php b/tests/phpunit/tests/query/homePageCpt.php
new file mode 100644
index 0000000000000..c88325d8a5b18
--- /dev/null
+++ b/tests/phpunit/tests/query/homePageCpt.php
@@ -0,0 +1,232 @@
+ true,
+ 'show_in_home_page_list' => true,
+ )
+ );
+
+ register_post_type(
+ self::NOT_ALLOWED,
+ array(
+ 'public' => true,
+ 'show_in_home_page_list' => false,
+ )
+ );
+ }
+
+ /**
+ * Creates one post of each type used by these tests.
+ */
+ private function create_posts() {
+ self::factory()->post->create();
+ self::factory()->post->create( array( 'post_type' => self::ALLOWED ) );
+ self::factory()->post->create( array( 'post_type' => self::NOT_ALLOWED ) );
+ }
+
+ /**
+ * Returns the unique post types of the posts in a query.
+ *
+ * @param WP_Query $query Query to inspect.
+ * @return string[] Post types found in the query results.
+ */
+ private function get_result_post_types( WP_Query $query ) {
+ return array_values( array_unique( wp_list_pluck( $query->posts, 'post_type' ) ) );
+ }
+
+ public function test_post_is_in_home_page_list_by_default() {
+ $post_types = get_home_page_post_types();
+
+ $this->assertContains( 'post', $post_types, 'The post post type should be in the home page list.' );
+ $this->assertNotContains( 'page', $post_types, 'The page post type should not be in the home page list.' );
+ $this->assertContains( self::ALLOWED, $post_types, 'The allowed post type should be in the home page list.' );
+ $this->assertNotContains( self::NOT_ALLOWED, $post_types, 'The denied post type should not be in the home page list.' );
+ }
+
+ public function test_filter_cannot_add_pages_or_unregistered_post_types() {
+ add_filter(
+ 'post_types_allowed_on_home_page',
+ static function ( $post_types ) {
+ return array_merge( $post_types, array( 'page', 'wptests_missing' ) );
+ }
+ );
+
+ $post_types = get_home_page_post_types();
+
+ $this->assertNotContains( 'page', $post_types, 'The page post type should be removed.' );
+ $this->assertNotContains( 'wptests_missing', $post_types, 'An unregistered post type should be removed.' );
+ }
+
+ /**
+ * @covers ::sanitize_option
+ */
+ public function test_show_archive_on_front_can_be_saved_as_allowed_post_type() {
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ $this->assertSame( self::ALLOWED, get_option( 'show_archive_on_front' ) );
+ }
+
+ /**
+ * @dataProvider data_not_allowed_values
+ *
+ * @covers ::sanitize_option
+ *
+ * @param mixed $value Value that is not an allowed post type.
+ */
+ public function test_show_archive_on_front_falls_back_to_post( $value ) {
+ update_option( 'show_archive_on_front', self::ALLOWED );
+ update_option( 'show_archive_on_front', $value );
+
+ $this->assertSame( 'post', get_option( 'show_archive_on_front' ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_not_allowed_values() {
+ return array(
+ 'a denied post type' => array( self::NOT_ALLOWED ),
+ 'the page post type' => array( 'page' ),
+ 'an unregistered type' => array( 'wptests_missing' ),
+ 'no submitted value' => array( null ),
+ );
+ }
+
+ public function test_default_home_shows_posts() {
+ $this->create_posts();
+
+ $this->go_to( home_url( '/' ) );
+
+ $this->assertTrue( is_home(), 'The home page should be the blog home.' );
+ $this->assertTrue( is_front_page(), 'The home page should be the front page.' );
+ $this->assertSame( '', get_query_var( 'post_type' ), 'The post_type query var should not be set.' );
+ $this->assertSame( array( 'post' ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_home_with_allowed_post_type_shows_that_post_type() {
+ $this->create_posts();
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ $this->go_to( home_url( '/' ) );
+
+ $this->assertTrue( is_home(), 'The home page should be the blog home.' );
+ $this->assertTrue( is_front_page(), 'The home page should be the front page.' );
+ $this->assertSame( self::ALLOWED, get_query_var( 'post_type' ) );
+ $this->assertSame( array( self::ALLOWED ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_second_home_page_shows_allowed_post_type() {
+ $this->create_posts();
+ $this->create_posts();
+ update_option( 'posts_per_page', 1 );
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ $this->go_to( home_url( '/?paged=2' ) );
+
+ $this->assertTrue( is_home(), 'The second page should be the blog home.' );
+ $this->assertSame( array( self::ALLOWED ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_post_types_allowed_on_home_page_filter_can_add_post_type() {
+ $this->create_posts();
+
+ add_filter(
+ 'post_types_allowed_on_home_page',
+ static function ( $post_types ) {
+ $post_types[] = self::NOT_ALLOWED;
+ return $post_types;
+ }
+ );
+
+ update_option( 'show_archive_on_front', self::NOT_ALLOWED );
+
+ $this->go_to( home_url( '/' ) );
+
+ $this->assertTrue( is_front_page(), 'The home page should be the front page.' );
+ $this->assertSame( self::NOT_ALLOWED, get_query_var( 'post_type' ) );
+ $this->assertSame( array( self::NOT_ALLOWED ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_post_types_allowed_on_home_page_filter_can_remove_post_type() {
+ $this->create_posts();
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ add_filter(
+ 'post_types_allowed_on_home_page',
+ static function ( $post_types ) {
+ return array_diff( $post_types, array( self::ALLOWED ) );
+ }
+ );
+
+ $this->go_to( home_url( '/' ) );
+
+ $this->assertSame( '', get_query_var( 'post_type' ), 'The post_type query var should not be set.' );
+ $this->assertSame( array( 'post' ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_explicit_post_type_query_var_is_not_overridden() {
+ $this->create_posts();
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ $this->go_to( home_url( '/?post_type=post' ) );
+
+ $this->assertSame( 'post', get_query_var( 'post_type' ) );
+ $this->assertSame( array( 'post' ), $this->get_result_post_types( $GLOBALS['wp_query'] ) );
+ }
+
+ public function test_secondary_query_without_post_type_still_returns_posts() {
+ $this->create_posts();
+ update_option( 'show_archive_on_front', self::ALLOWED );
+
+ // Same shape as the query used by the Recent Posts widget.
+ $query = new WP_Query( array( 'posts_per_page' => 5 ) );
+
+ $this->assertSame( array( 'post' ), $this->get_result_post_types( $query ) );
+ }
+
+ public function test_static_front_page_is_not_affected() {
+ $this->create_posts();
+ $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
+ update_option( 'show_archive_on_front', self::ALLOWED );
+ update_option( 'show_on_front', 'page' );
+ update_option( 'page_on_front', $page_id );
+
+ $this->go_to( home_url( '/' ) );
+
+ $this->assertTrue( is_front_page(), 'The static page should be the front page.' );
+ $this->assertTrue( is_page( $page_id ), 'The static page should be queried.' );
+ $this->assertFalse( is_home(), 'The static front page should not be the blog home.' );
+ }
+}
|