[wp-trac] [WordPress Trac] #40325: Potential bug with the “get_post_type” function

WordPress Trac noreply at wordpress.org
Sun Dec 13 20:41:20 UTC 2020


#40325: Potential bug with the “get_post_type” function
-------------------------------+------------------------------
 Reporter:  ancientro          |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:  4.7.3
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by engahmeds3ed):

 I can replicate the issue on those two scenarios:-


 {{{
 add_action('before_delete_post', 'ahmed_delete_post');
 function ahmed_delete_post($post_id) {
         $case1 = get_post_type();
         die(var_dump($case1));
 }
 }}}

 I added this code into my theme `functions.php` and I can see that it
 returns false in both cases and I can see the reason for that as follows:-

 https://github.com/wordpress/wordpress-
 develop/blob/bcc26de235cea8d42ad295c4c5aa999379b84cca/src/wp-
 admin/edit.php#L161

 here we get the posts with their ID

 https://github.com/wordpress/wordpress-
 develop/blob/37f8276a18633c8b465211469d77a2dddc0a7bc1/src/wp-
 includes/post.php#L763

 and here we don't set $GLOBALS['post']

 so when `get_post_type` calls `get_post` without the argument `$post`, it
 tries to assign

 $post = $GLOBALS['post'];

 and here in this case $GLOBALS['post'] is null.

 https://github.com/wordpress/wordpress-
 develop/blob/37f8276a18633c8b465211469d77a2dddc0a7bc1/src/wp-
 includes/post.php#L765

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


More information about the wp-trac mailing list