Repository: Automattic/newspack-theme
Pull request #2484: fix: add filter to update date cpt
Author: login: laurelfulford
Labels: name: [Status] Approved
description: The pull request has been reviewed and is ready to merge
name: released
name: released on @alpha
Created: 2025-04-16T22:25:42Z | Merged: 2025-04-23T19:55:15Z

### All Submissions:

* [x] Have you followed the [Newspack Contributing guideline](https://github.com/Automattic/newspack-theme/blob/trunk/.github/CONTRIBUTING.md)?
* [x] Does your code follow the [WordPress' coding standards](https://make.wordpress.org/core/handbook/best-practices/coding-standards/) and [VIP Go coding standards](https://vip.wordpress.com/documentation/vip-go/code-review-blockers-warnings-notices/)?
* [x] Have you checked to ensure there aren't other open [Pull Requests](../../pulls) for the same update/change?

### Changes proposed in this Pull Request:

We recently [limited the updated date to only showing on posts](https://github.com/Automattic/newspack-theme/pull/2453), which had some unexpected outcomes on sites with custom post types.

This PR adds a filter for that functionality, so individuals can "opt in" to showing the updated date on other post types. Note: this will only work on post types that show the regular published date already, since we filter that. 

See 1200550061930446-as-1209980970331850

### How to test the changes in this Pull Request:

1. Apply this PR and run `npm run build`.
2. Retest this functionality on regular posts just to make sure it doesn't cause issues:
* Navigate to Customizer > Template Settings > Post Settings, and enable `Show "last updated" date on single posts`. This site-wide setting is only applied to posts edited 24 hours after they were published, so you may need to edit an older post to see it. 

![CleanShot 2025-04-16 at 14 55 15](https://github.com/user-attachments/assets/8210d690-d6fc-418c-ae22-b9fde6628d98)

* Edit a post that shows the updated date, and confirm you have a toggle to "Hide last updated date".
* Enable this toggle and click "Save"; confirm that the updated date is now hidden on the front-end.
* Navigate back to the Customizer, and disable `Show "last updated" date on single posts`.
* Edit any post, and confirm there's a "Show last updated date" toggle in the sidebar. Turn it on and click "Save".
* Confirm that the updated post is now showing the updated date; this will happen without delay because it can be toggled on/off, and less likely to be showing the date in error (like after a minor typo fix). 

3. Now test with a different post type that shows a date. A good option for this is newsletters published to the front end:
* Edit a published newsletter and confirm you don't have a toggle in the right sidebar (either for showing or hiding the updated date). (You can also send a test newsletter and set the published date to the past if you don't have a published newsletter older than 1 day).
* Add a mini-plugin to filter the post types used by the updated date functionality -- like:

```
function support_newspack_updated_date_options( $post_types ) {
	$post_types[] = 'newspack_nl_cpt';
	return $post_types;
}
add_filter( 'newspack_updated_date_supported_post_types', 'support_newspack_updated_date_options' );
```
* Refresh your newsletter in the editor and confirm you now have the 'Show last updated date' toggle - turn it on and click Save.
* Confirm you get the updated date on the front-end.
* Double-check one of your posts and confirm this option is still visible/still works as expected.
* Navigate to Customizer > Template Settings > Post Settings, and enable `Show "last updated" date on single posts`.
* View your newsletter post again and confirm the Updated Date is visible.
* Edit your newsletter post; confirm you have an option to "Hide last updated date". Enable it, click Save, and confirm it hides the updated date on the front-end.
* Double-check one of your posts and confirm this option is still visible/still works as expected.


### Other information:

* [x] Have you added an explanation of what your changes do and why you'd like us to include them?
* [ ] Have you written new tests for your changes, as applicable?
* [ ] Have you successfully ran tests with your changes locally?

<!-- Mark completed items with an [x] -->


---
- To see the specific tasks where the Asana app for GitHub is being used, see below:
  - https://app.asana.com/0/0/1209980970331850


Diff:
diff --git a/newspack-theme/functions.php b/newspack-theme/functions.php
index 59a177c1d..a2679d0c1 100755
--- a/newspack-theme/functions.php
+++ b/newspack-theme/functions.php
@@ -936,25 +936,29 @@ function newspack_register_meta() {
 		)
 	);
 
-	register_post_meta(
-		'post',
-		'newspack_hide_updated_date',
-		array(
-			'show_in_rest' => true,
-			'single'       => true,
-			'type'         => 'boolean',
-		)
-	);
+	$updated_date_post_types = newspack_get_updated_date_supported_post_types();
 
-	register_post_meta(
-		'post',
-		'newspack_show_updated_date',
-		array(
-			'show_in_rest' => true,
-			'single'       => true,
-			'type'         => 'boolean',
-		)
-	);
+	foreach ( $updated_date_post_types as $post_type ) {
+		register_post_meta(
+			$post_type,
+			'newspack_hide_updated_date',
+			array(
+				'show_in_rest' => true,
+				'single'       => true,
+				'type'         => 'boolean',
+			)
+		);
+
+		register_post_meta(
+			$post_type,
+			'newspack_show_updated_date',
+			array(
+				'show_in_rest' => true,
+				'single'       => true,
+				'type'         => 'boolean',
+			)
+		);
+	}
 
 	register_post_meta(
 		'page',
@@ -1187,9 +1191,9 @@ function newspack_get_post_toggle_post_types() {
 	$hide_date_post_types = [];
 	$show_date_post_types = [];
 	if ( true === get_theme_mod( 'post_updated_date', false ) ) {
-		$hide_date_post_types[] = 'post';
+		$hide_date_post_types = newspack_get_updated_date_supported_post_types();
 	} else {
-		$show_date_post_types[] = 'post';
+		$show_date_post_types = newspack_get_updated_date_supported_post_types();
 	}
 
 	return array(
diff --git a/newspack-theme/inc/template-functions.php b/newspack-theme/inc/template-functions.php
index 7ae6040b1..093af690a 100755
--- a/newspack-theme/inc/template-functions.php
+++ b/newspack-theme/inc/template-functions.php
@@ -690,13 +690,29 @@ function newspack_convert_modified_to_time_ago( $post_time, $format, $post ) {
 	return newspack_math_to_time_ago( $post_time, $format, $post, true );
 }
 
+/**
+ * Return a filterable array of supported post types for the updated date functionality.
+ *
+ * @return array Array of post type slugs.
+ */
+function newspack_get_updated_date_supported_post_types() {
+	/**
+	 * Filter post types that support the updated date functionality.
+	 *
+	 * @return array Array of post type slugs.
+	 */
+	return apply_filters( 'newspack_updated_date_supported_post_types', array( 'post' ) );
+}
+
 /**
  * Check whether updated date should be displayed.
  */
 function newspack_should_display_updated_date() {
-	if ( ! is_singular( 'post' ) ) {
+	$supported_post_types = newspack_get_updated_date_supported_post_types();
+	if ( ! is_singular( $supported_post_types ) ) {
 		return false;
 	}
+
 	$show_updated_date_sitewide = get_theme_mod( 'post_updated_date', false );
 
 	$hide_updated_date_post     = get_post_meta( get_the_ID(), 'newspack_hide_updated_date', true );
diff --git a/newspack-theme/js/src/post-meta-toggles.js b/newspack-theme/js/src/post-meta-toggles.js
index d81512d3b..b416bb47b 100644
--- a/newspack-theme/js/src/post-meta-toggles.js
+++ b/newspack-theme/js/src/post-meta-toggles.js
@@ -38,7 +38,7 @@ const PostStatusExtensions = ( { meta, postType, updateMetaValue } ) => {
 
 	return (
 		<PluginPostStatusInfo className="newspack__post-meta-toggles">
-			{ hideDate && 'post' === postType && (
+			{ hideDate && ( 0 <= hide_date.indexOf( postType ) ) && (
 				<div>
 					<label htmlFor="hide_updated_date">{ __( 'Hide last updated date', 'newspack' ) }</label>
 					<FormToggle
@@ -50,7 +50,7 @@ const PostStatusExtensions = ( { meta, postType, updateMetaValue } ) => {
 					/>
 				</div>
 			) }
-			{ showDate && 'post' === postType && (
+			{ showDate && ( 0 <= show_date.indexOf( postType ) ) && (
 				<div>
 					<label htmlFor="show_updated_date">{ __( 'Show last updated date', 'newspack' ) }</label>
 					<FormToggle


Reviews:
user: login: leogermani
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-22T20:12:28Z
user: login: laurelfulford
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-22T22:35:46Z
user: login: leogermani
body: Looks good
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T19:31:51Z

Review comments:
diff_hunk: @@ -690,13 +690,24 @@ function newspack_convert_modified_to_time_ago( $post_time, $format, $post ) {
 	return newspack_math_to_time_ago( $post_time, $format, $post, true );
 }
 
+/**
+ * Return a filterable array of supported post types for the updated date functionality.
+ *
+ * @return array Array of post type slugs.
+ */
+function newspack_get_updated_date_supported_post_types() {
+	return apply_filters( 'newspack_updated_date_supported_post_types', array( 'post' ) );
path: newspack-theme/inc/template-functions.php
user: login: leogermani
body: add a doc block to the filter
created_at: 2025-04-22T20:12:28Z
author_association: CONTRIBUTOR
diff_hunk: @@ -690,13 +690,24 @@ function newspack_convert_modified_to_time_ago( $post_time, $format, $post ) {
 	return newspack_math_to_time_ago( $post_time, $format, $post, true );
 }
 
+/**
+ * Return a filterable array of supported post types for the updated date functionality.
+ *
+ * @return array Array of post type slugs.
+ */
+function newspack_get_updated_date_supported_post_types() {
+	return apply_filters( 'newspack_updated_date_supported_post_types', array( 'post' ) );
path: newspack-theme/inc/template-functions.php
user: login: laurelfulford
body: Thanks @leogermani! Just let me know if what I did ended up being to redundant with the function's doc block!
created_at: 2025-04-22T22:35:46Z
author_association: CONTRIBUTOR

PR comments:
user: login: laurelfulford
created_at: 2025-04-23T19:54:57Z
body: Thanks @leogermani! 
author_association: CONTRIBUTOR
user: login: github-actions[bot]
created_at: 2025-04-23T19:55:26Z
body: Hey @laurelfulford, good job getting this PR merged! :tada:

Now, the `needs-changelog` label has been added to it.

Please check if this PR needs to be included in the "Upcoming Changes" and "Release Notes" doc. If it doesn't, simply remove the label.

If it does, please add an entry to our shared document, with screenshots and testing instructions if applicable, then remove the label.

Thank you! :heart:
author_association: NONE
user: login: matticbot
created_at: 2025-05-23T15:45:46Z
body: :tada: This PR is included in version 2.9.1-alpha.1 :tada:

The release is available on [GitHub release](https://github.com/Automattic/newspack-theme/releases/tag/v2.9.1-alpha.1)

Your **[semantic-release](https://github.com/semantic-release/semantic-release)** bot :package::rocket:
author_association: CONTRIBUTOR
user: login: matticbot
created_at: 2025-06-02T19:33:02Z
body: :tada: This PR is included in version 2.9.1 :tada:

The release is available on [GitHub release](https://github.com/Automattic/newspack-theme/releases/tag/v2.9.1)

Your **[semantic-release](https://github.com/semantic-release/semantic-release)** bot :package::rocket:
author_association: CONTRIBUTOR