Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#28135 closed defect (bug) (fixed)

wp_insert_post returns error for custom post type in 3.9.0

Reported by: capit0l's profile capit0l Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 4.0 Priority: normal
Severity: major Version: 3.9
Component: Posts, Post Types Keywords: good-first-bug has-patch
Focuses: Cc:

Description

I'm trying to insert post for custom post type by function wp_insert_post it returns error. But functions still works, post is added to database.

This is my code:

$current_user = wp_get_current_user();
$name = $_REQUEST['paymetn_m_name'] . ' ' . $_REQUEST['paymetn_m_sname'];

$post = array(
    'post_title' => $name,
    'post_type' => 'orders',
    'post_status' => 'publish',
    'post_author' => $current_user->ID
);

wp_insert_post( $post );

This is error:

Notice: Trying to get property of non-object in /Users/capit0l/Sites/technostore/wp-includes/post.php on line 2187
Notice: Trying to get property of non-object in /Users/capit0l/Sites/technostore/wp-includes/post.php on line 2187

Problem is in this function:

function _count_posts_cache_key( $type = 'post', $perm = '' ) {
	$cache_key = 'posts-' . $type;
	if ( 'readable' == $perm && is_user_logged_in() ) {
		$post_type_object = get_post_type_object( $type );
		if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
			$cache_key .= '_' . $perm . '_' . get_current_user_id();
		}
	}
	return $cache_key;
}

As a temporary solution I added @ in front of wp_insert_post function.

For extra information, i'm using OS X 10.9.2, PHP Version 5.4.24.

Attachments (4)

28135.ew_holmes.patch (744 bytes) - added by ew_holmes 10 years ago.
Checking for post type before trying to create a post.
28135.2.diff (553 bytes) - added by michalzuber 10 years ago.
Show what post type doesn't exist
28135_test.diff (541 bytes) - added by tanner m 10 years ago.
Here is a unit test for this issue. Hope it helps!
28135.diff (1.1 KB) - added by jesin 10 years ago.
Checks if $post_type_object is empty or not

Download all attachments as: .zip

Change History (18)

#1 @capit0l
10 years ago

  • Severity changed from normal to major

#2 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 3.9.1

The function was added in [27081], moving for investigation.

Sounds like your code needs to wait until register_post_type() fires, but if that didn't cause a notice before, we should probably fix it.

Last edited 10 years ago by SergeyBiryukov (previous) (diff)

#3 @nacin
10 years ago

  • Milestone changed from 3.9.1 to 3.9.2

#4 @jesin
10 years ago

As SergeyBiryukov pointed out I was able to get this notice only if the post type is registered after wp_insert_post().

We need a post_type_exists() check inside wp_insert_post(). I find that that this function creates a post even if the $post_type is registered nowhere. And this inserted post has the non-registered post type in the database.

I am interested to submit a patch for this but how is this error handled? Is a WP_Error object returned, fallback to default post type or _doing_it_wrong()? I think WP_Error is the right option.

@ew_holmes
10 years ago

Checking for post type before trying to create a post.

#5 @ew_holmes
10 years ago

  • Keywords has-patch added

#6 @ew_holmes
10 years ago

  • Keywords needs-testing added

@michalzuber
10 years ago

Show what post type doesn't exist

#7 @michalzuber
10 years ago

$post_type_object = get_post_type_object( $type ); is NULL if post type doesn't exists.

@ew_holmes patch worked, just added post type slug into the message.

@tanner m
10 years ago

Here is a unit test for this issue. Hope it helps!

This ticket was mentioned in IRC in #wordpress-dev by tannermoushey. View the logs.


10 years ago

#9 @SergeyBiryukov
10 years ago

Was going to commit 28135.2.diff and 28135_test.diff, but then realized we already have a ticket for that: #27335 (closed as maybelater).

Let's just avoid the notice introduced in [27081].

#10 @tanner m
10 years ago

Yup, I agree with the concerns given in #27335. Thanks Sergey.

#11 @SergeyBiryukov
10 years ago

  • Keywords needs-patch good-first-bug added; has-patch needs-testing removed

We should just check if $post_type_object is not empty in _count_posts_cache_key().

@jesin
10 years ago

Checks if $post_type_object is empty or not

#12 @jesin
10 years ago

  • Keywords has-patch added; needs-patch removed

Another PHP notice is triggered on MS setups:

Notice: Trying to get property of non-object in /var/www/html/wp-includes/ms-blogs.php on line 877

The 28135.diff patch fixes both of them.

#13 @SergeyBiryukov
10 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 29318:

Avoid PHP notices in _count_posts_cache_key(), _update_blog_date_on_post_publish(), and _update_blog_date_on_post_delete() if post type is not registered.

props jesin.
fixes #28135.

#14 @SergeyBiryukov
10 years ago

  • Milestone changed from 3.9.2 to 4.0
Note: See TracTickets for help on using tickets.