May have found a bug with the “get_post_type” function
-
I have encountered a weird behavior in the WP code and I wanted to report it, in case it is a bug.
I have a custom post type created. A function is added to the
before_delete_posthook. In this function, I am verifying the current post type.I used to verify it with the
get_post_type()function but I have found a problem with that in some cases. Now, I have replaced it withglobal $post_type;class My_Example { public function build_admin() { add_action('before_delete_post', array($this, 'delete_post')); } public function delete_post($post_id) { $case1 = get_post_type(); global $post_type; $case2 = $post_type; } }On the posts listing page, in the Trash table, there are 3 says of deleting posts:
- 1. The Delete Permanently button for each post.
- 2. The Empty Trash button.
- 3. And through Bulk Actions > Delete Permanently > Apply.
The
get_post_type()function returns the proper post type name only in the 1st case. For the other 2 cases, it returnsfalse.However,
global $post_typereturns the proper value in all cases.I don’t know if it is a bug but it seems that way on the surface. If the global post type variable is set well, then the function should be able to return it too but I am not sure if something else it at play here.
A bit more info:
Looking over the source code for the
get_post_typefunction in Code Reference, I see that it is using theget_postfunction to retrieve an object, instance ofWP_Post, from which it gets the post type.So, the value is retrieved from different locations in the two examples.
The topic ‘May have found a bug with the “get_post_type” function’ is closed to new replies.