[wp-trac] [WordPress Trac] #12955: Add get_post filter

WordPress Trac noreply at wordpress.org
Thu May 29 05:22:23 UTC 2025


#12955: Add get_post filter
-------------------------------------------------+-------------------------
 Reporter:  JohnLamansky                         |       Owner:  (none)
     Type:  feature request                      |      Status:  new
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Posts, Post Types                    |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch dev-feedback needs-        |     Focuses:
  refresh                                        |
-------------------------------------------------+-------------------------

Comment (by mamunur105):

 Is it possible ?

 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
 }
 }}}

 Hook

 {{{
 function get_post_type( $post = null ) {
         $post = get_post( $post );

         if ( $post ) {
                 $post_type = apply_filters( 'get_post_type_filtered',
 $post->post_type, $post );
                 return $post_type;
         }

         return false;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/12955#comment:87>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list