Make WordPress Core

Opened 8 years ago

Last modified 4 years ago

#40325 new defect (bug)

Potential bug with the “get_post_type” function

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

Description

I have encountered a weird behavior in the WP code, and I want to report it, in case it is a bug.

I have posted it in the Support forum first and I was advised to make a ticket here.

Replication:

I have a custom post type created. A function is added to the before_delete_post hook. 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 with global $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 returns false.

However, global $post_type returns 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_type function in Code Reference, I see that it is using the get_post function to retrieve an object, instance of WP_Post, from which it gets the post type.

So, the value is retrieved from different locations in the two examples.

Change History (4)

#1 @johnbillion
8 years ago

  • Keywords reporter-feedback added

Thanks for the ticket, @ancientro. Welcome to WordPress Trac.

Does the code function as expected if you pass the $post_id parameter into get_post_type()?

#2 @ancientro
8 years ago

I tested and if I pass the $post_id parameter into get_post_type(), the function works well in all cases and returns the proper post type name.

#3 @engahmeds3ed
4 years ago

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 $GLOBALSpost?

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

$post = $GLOBALS['post'];

and here in this case $GLOBALSpost? is null.

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

Last edited 4 years ago by engahmeds3ed (previous) (diff)

#4 @hellofromTonya
4 years ago

  • Keywords reporter-feedback removed
Note: See TracTickets for help on using tickets.