WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32585 closed defect (bug) (fixed)

Programmatically using wp_insert_post with post_author=0 does not work when user is logged in

Reported by: ericdaams Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch needs-testing needs-unit-tests
Focuses: Cc:

Description

Passing a post_author of 0 to wp_insert_post does not work if the user is logged in.

Reproducible with:

add_action( 'init', function() {
	$args = array(
		'post_title' => 'test', 
		'post_author' => 0
	);

	$post_id = wp_insert_post( $args );

	echo get_post_field( 'post_author', $post_id ); // post_author is 1
});

post_author returns 1, despite post_author being set explicitly to 0.

Attachments (2)

post.diff (735 bytes) - added by ericdaams 3 years ago.
Using isset instead of empty ensures that post_author of 0 is correctly handled.
32585.diff (1.8 KB) - added by wonderboymusic 3 years ago.

Download all attachments as: .zip

Change History (7)

@ericdaams
3 years ago

Using isset instead of empty ensures that post_author of 0 is correctly handled.

#1 @ericdaams
3 years ago

  • Keywords has-patch added

#2 @johnbillion
3 years ago

  • Keywords needs-testing needs-unit-tests added
  • Version 4.2.2 deleted

Thanks for the patch, ericdaams!

This will need some unit tests.

#3 @SergeyBiryukov
3 years ago

  • Summary changed from Programatically using wp_insert_post with post_author=0 does not work when user is logged in to Programmatically using wp_insert_post with post_author=0 does not work when user is logged in

@wonderboymusic
3 years ago

#4 @wonderboymusic
3 years ago

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

In 34085:

In wp_insert_post(), when setting $post_author, use isset() instead of ! empty() to allow 0 to be passed as the value for $post_author.

Adds unit tests.

Props ericdaams, wonderboymusic.
Fixes #32585.

#5 @ocean90
3 years ago

  • Milestone changed from Awaiting Review to 4.4
Note: See TracTickets for help on using tickets.