Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions src/wp-admin/includes/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ function options_reading_add_js() {
<script>
jQuery( function($) {
var section = $('#front-static-pages'),
staticPage = section.find('input:radio[value="page"]'),
selects = section.find('select'),
selects = section.find('.staticPages select'),
homepage_types = section.find('select#homepage_types')
check_disabled = function(){
selects.prop( 'disabled', ! staticPage.prop('checked') );
selects.prop( 'disabled', homepage_types.val() !== "page" );
};
check_disabled();
section.find( 'input:radio' ).on( 'change', check_disabled );
homepage_types.on( 'change', check_disabled );
} );
</script>
<?php
Expand Down
102 changes: 74 additions & 28 deletions src/wp-admin/options-reading.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@
if ( ! is_utf8_charset() ) {
add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) );
}
/** This filter is documented in wp-includes/class-wp-query.php:1057 */
$post_types_allowed_on_home_page = apply_filters( 'post_types_allowed_on_home_page', array_keys( get_post_types( array( 'show_in_home_page_list' => true ) ) ) );
$args = array(
'post_type' => $post_types_allowed_on_home_page,
'post_status' => 'publish',
'fields' => 'ids',
);
$allowed_pages = new WP_Query( $args );
?>

<?php if ( ! get_pages() ) : ?>
Expand All @@ -76,51 +84,89 @@
if ( 'posts' !== get_option( 'show_on_front' ) ) :
update_option( 'show_on_front', 'posts' );
endif;
endif;

else :
if ( 'page' === get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) ) {
update_option( 'show_on_front', 'posts' );
}
if ( 'page' === get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) ) {
update_option( 'show_on_front', 'posts' );
}

$your_homepage_displays_title = __( 'Your homepage displays' );
?>
$your_homepage_displays_title = __( 'Your homepage displays' );
?>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><?php echo $your_homepage_displays_title; ?></th>
<td id="front-static-pages"><fieldset>
<legend class="screen-reader-text"><span><?php echo $your_homepage_displays_title; ?></span></legend>
<p><label>
<input name="show_on_front" type="radio" value="posts" class="tog" <?php checked( 'posts', get_option( 'show_on_front' ) ); ?> />
<?php _e( 'Your latest posts' ); ?>
<?php
$allowed_archives_on_home_page = $post_types_allowed_on_home_page;

Check failure on line 102 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
unset( $allowed_archives_on_home_page['page'] );

Check failure on line 103 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are pages specifically excluded?

Copy link
Author

@pbearne pbearne Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a list of CPT that can be used as a home archive. This can not be pages
Pages shouldn't be set, but I'm protecting the settings here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, got it. maybe add an inline comment to clarify. confused me when reviewing the code.

if( 1 === count( $allowed_archives_on_home_page ) ) {

Check failure on line 104 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

No space before opening parenthesis is prohibited

Check failure on line 104 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Space after opening control structure is required

Check failure on line 104 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Line indented incorrectly; expected 0 tabs, found 2

Check failure on line 104 in src/wp-admin/options-reading.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
_e( sprintf( 'Your latest %s', get_post_type_object( $allowed_archives_on_home_page[0] )->labels->name ) );
?><input name="show_on_front" type="hidden" value="<?php echo esc_attr( $allowed_archives_on_home_page[0] ); ?>" /> <?php
} else {
$output = "<select name='show_on_front' id='homepage_types'>\n";
$output .= sprintf( '<option value="page" %s>A static page (select below)</option>', selected( -1, strtolower( get_option( 'show_on_front' ) ), false ) ) ;
foreach ( $allowed_archives_on_home_page as $post_type ){
$post_type = ( 'posts' === $post_type ) ? 'post' : $post_type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code came from the old code, so I kept it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, its just ugly hence "oof". fine to keep.

$output .= sprintf( '<option value="%s" %s>%s</option>',
$post_type,
selected( $post_type, strtolower( get_option( 'show_on_front' ) ),false ),
sprintf( __( 'Your latest %s' ), get_post_type_object( $post_type )->labels->name )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we always assume get_post_type_object( $post_type )->labels->name exists? if not, maybe add some defensive checking before using.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, it looks like it is always set.

src/wp-includes/class-wp-post-type.php:567

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for confirming.

) ;
}
$output .= "</select>\n";

echo $output;
}
?>
</label>
</p>
<?php if ( empty( $allowed_pages->posts ) ) : ?>
<p> <?php
$content_type_links = array();
foreach ( $post_types_allowed_on_home_page as $type ){
$content_type_links[] = sprintf( '<a href="%s">%s</a>',
'edit.php?post_type=' . $type, ucfirst( $type ) );
}
printf(
/* translators: %s: URL to Pages screen. */
__( 'No selectable pages found! Add/Publish at least one of these content types %s' ),
implode( ', ', $content_type_links )
);
?>
</p>
<?php else : ?>
<p><label>
<input name="show_on_front" type="radio" value="page" class="tog" <?php checked( 'page', get_option( 'show_on_front' ) ); ?> />
<?php
printf(
/* translators: %s: URL to Pages screen. */
__( 'A <a href="%s">static page</a> (select below)' ),
'edit.php?post_type=page'
);
?>
</label>
</p>
<ul>
<ul class="staticPages">
<li><label for="page_on_front">
<?php

$output = "<select name='page_on_front' >\n";
foreach ( $post_types_allowed_on_home_page as $post_type ){
$post_type = ( 'posts' === $post_type ) ? 'post' : $post_type;
$output .= sprintf( '<optgroup label="%s">', get_post_type_object( $post_type )->labels->name );
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
);
$cpt_posts = new WP_Query( $args );
foreach ( $cpt_posts->posts as $cpt_post ) {
$output .= sprintf( '<option value="%s" %s>%s</option>',
$cpt_post->ID,
selected( $cpt_post->ID, get_option( 'page_on_front' ),false ),
$cpt_post->post_name ) ;
}
$output .= '</optgroup>';
}
$output .= "</select>\n";


printf(
/* translators: %s: Select field to choose the front page. */
__( 'Homepage: %s' ),
wp_dropdown_pages(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we use wp_dropdown_pages elsewhere? wondering out loud if we should update the function directly rather than replacing it here inline.

array(
'name' => 'page_on_front',
'echo' => 0,
'show_option_none' => __( '&mdash; Select &mdash;' ),
'option_none_value' => '0',
'selected' => get_option( 'page_on_front' ),
)
)
);
__( 'Homepage: %s' ), $output );
?>
</label></li>
<li><label for="page_for_posts">
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 @@ -146,6 +146,7 @@
'show_on_front',
'page_on_front',
'page_for_posts',
'show_archive_on_front',
'blog_public',
),
'writing' => array(
Expand Down
64 changes: 64 additions & 0 deletions src/wp-content/themes/twentytwenty/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,67 @@
*/
return apply_filters( 'twentytwenty_get_elements_array', $elements );
}



//add_filter( 'post_types_allowed_on_home_page', 'rsadfd_post_types_allowed_on_home_page');
function rsadfd_post_types_allowed_on_home_page( $posts ){

Check failure on line 830 in src/wp-content/themes/twentytwenty/functions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Expected 1 space before opening brace; found 0

$posts[] = 'book';
return $posts;

Check failure on line 833 in src/wp-content/themes/twentytwenty/functions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
}


/**
* Register a custom post type called "book".
*
* @see get_post_type_labels() for label keys.
*/
function wpdocs_codex_book_init() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this section for testing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I need to remove it

$labels = array(
'name' => _x( 'Books', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'Book', 'Post type singular name', 'textdomain' ),
'menu_name' => _x( 'Books', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Book', 'Add New on Toolbar', 'textdomain' ),
'add_new' => __( 'Add New', 'textdomain' ),
'add_new_item' => __( 'Add New Book', 'textdomain' ),
'new_item' => __( 'New Book', 'textdomain' ),
'edit_item' => __( 'Edit Book', 'textdomain' ),
'view_item' => __( 'View Book', 'textdomain' ),
'all_items' => __( 'All Books', 'textdomain' ),
'search_items' => __( 'Search Books', 'textdomain' ),
'parent_item_colon' => __( 'Parent Books:', 'textdomain' ),
'not_found' => __( 'No books found.', 'textdomain' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'textdomain' ),
'featured_image' => _x( 'Book Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
'archives' => _x( 'Book archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
'insert_into_item' => _x( 'Insert into book', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
'uploaded_to_this_item' => _x( 'Uploaded to this book', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
'filter_items_list' => _x( 'Filter books list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
'items_list_navigation' => _x( 'Books list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
'items_list' => _x( 'Books list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'show_in_home_page_list'=> true,

Check failure on line 882 in src/wp-content/themes/twentytwenty/functions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Expected 1 space before "=>"; 0 found
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);

register_post_type( 'book', $args );
}

add_action( 'init', 'wpdocs_codex_book_init' );
48 changes: 48 additions & 0 deletions src/wp-content/themes/twentytwentyfive/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@
endif;
add_action( 'init', 'twentytwentyfive_register_block_bindings' );

/**
* Registers the 'books' custom post type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks lioke testing code that can be removed

*
* @since Twenty Twenty-Five 1.0
*
* @return void
*/
function twentytwentyfive_register_books_post_type() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'twentytwentyfive' ),
'singular_name' => _x( 'Book', 'post type singular name', 'twentytwentyfive' ),
'menu_name' => _x( 'Books', 'admin menu', 'twentytwentyfive' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'twentytwentyfive' ),
'add_new' => _x( 'Add New', 'book', 'twentytwentyfive' ),
'add_new_item' => __( 'Add New Book', 'twentytwentyfive' ),
'new_item' => __( 'New Book', 'twentytwentyfive' ),
'edit_item' => __( 'Edit Book', 'twentytwentyfive' ),
'view_item' => __( 'View Book', 'twentytwentyfive' ),
'all_items' => __( 'All Books', 'twentytwentyfive' ),
'search_items' => __( 'Search Books', 'twentytwentyfive' ),
'parent_item_colon' => __( 'Parent Books:', 'twentytwentyfive' ),
'not_found' => __( 'No books found.', 'twentytwentyfive' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'twentytwentyfive' ),
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'show_in_rest' => true,
'show_in_home_page_list' => true,
);

register_post_type( 'book', $args );
}
add_action( 'init', 'twentytwentyfive_register_books_post_type' );

if ( ! function_exists( 'twentytwentyfive_format_binding' ) ) :
/**
* Callback function for the post format name block binding source.
Expand All @@ -156,4 +202,6 @@
return get_post_format_string( $post_format_slug );
}
}
endif;

Check failure on line 205 in src/wp-content/themes/twentytwentyfive/functions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Expected 1 blank line at end of file; 3 found


27 changes: 26 additions & 1 deletion src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,28 @@ public function parse_query( $query = '' ) {
$this->is_home = true;
}

// this block control what is CPT is used for the home page archive list
if ( $this->is_home && 'page' !== get_option( 'show_on_front' ) && empty( $query_vars['post_type'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An inline comment explaining why this is being added here would be helpful (not obvious to me)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

/**
* Filters the CPT that can be displayed as home page achivle list.
*
* Only 'post' is set as default.
*
* @since 7.0.0
*
* @param string[] $search_columns Array of post-types.
*/
$post_types_allowed_on_home_page = apply_filters( 'post_types_allowed_on_home_page', array_keys( get_post_types( array( 'show_in_home_page_list' => true ) ) ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filters get documented once, other instances include a reference ( // This filter is documented in ...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

$front_page_post_type = get_option( 'show_on_front' );
if ( 'posts' === $front_page_post_type ) {
$front_page_post_type = 'post';
}

if ( in_array( $front_page_post_type, $post_types_allowed_on_home_page, true ) ) {
$query_vars['post_type'] = $front_page_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 Expand Up @@ -4470,8 +4492,11 @@ public function is_comment_feed() {
* @return bool Whether the query is for the front page of the site.
*/
public function is_front_page() {
/** This filter is documented in wp-includes/class-wp-query.php:1057 */
$post_types_allowed_on_home_page = apply_filters( 'post_types_allowed_on_home_page', array_keys( get_post_types( array( 'show_in_home_page_list' => true ) ) ) );

// Most likely case.
if ( 'posts' === get_option( 'show_on_front' ) && $this->is_home() ) {
if ( in_array( get_option( 'show_on_front' ), $post_types_allowed_on_home_page, true ) && $this->is_home() ) {
return true;
} elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' )
&& $this->is_page( get_option( 'page_on_front' ) )
Expand Down
Loading
Loading