Make WordPress Core

Opened 6 weeks ago

Closed 6 weeks ago

Last modified 6 weeks ago

#63500 closed enhancement (duplicate)

Add a filter hook to get_post() to allow modification of the returned WP_Post object

Reported by: mamunur105's profile mamunur105 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description (last modified by dd32)

The get_post() function in wp-includes/post.php is a widely used core function that returns a WP_Post object from a post ID or object. However, it currently offers no way to filter or modify the returned post object before it's used elsewhere.

This limits the flexibility for plugin and theme developers who may need to customize or inject data into post objects globally — for example, to:

  • Dynamically alter post titles or content
  • Add computed or cached meta fields
  • Modify fields based on user roles or context

### Suggestion:

Add a filter hook before the return statement in get_post(), like so:

$_post = apply_filters( 'get_post_object', $_post, $_post->ID );

Proposed location:

Just before the final return $_post; line inside get_post() in wp-includes/post.php.

Sometimes developers need to virtually adjust or override post data without modifying the database.

A common example is: if( 'product' === get_post_type( $id ) )

This condition is hard-coded and relies on the actual stored post type. However, there are valid use cases where a developer might want to treat a post as a different post type or override a field programmatically — for testing, compatibility layers, dynamic translations, or headless API adjustments.

Change History (2)

#1 @dd32
6 weeks ago

  • Component changed from General to Posts, Post Types
  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @mamunur105,

This request is being tracked in #12955

Note: Use of WP_Post fields directly is discouraged and the helper methods should be used instead, additionally, only modifying the contents of the WP_Post may not reflect through other helper methods.

#2 @mamunur105
6 weeks ago

The get_post_type() function in wp-includes/post.php is widely used throughout WordPress to retrieve the post type of a given post. However, it currently offers no hook to modify the returned value programmatically.

### Use Case:

In some advanced scenarios, developers may want to virtually override the post type for specific logic without changing the actual value stored in the database. For example:

if ( 'product' === get_post_type( $post_id ) ) {
    // Treat differently
}
Note: See TracTickets for help on using tickets.