WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#30454 closed defect (bug) (duplicate)

A slug can't be registered when it uses wp_insert_post function in wp_cron event

Reported by: horike Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

When I execute below program, the slug is empty.
If post_status is pending, It occurs.

add_filter( 'cron_schedules', function() {
	$schedules['panpanpan'] = array( 'interval' => 60, 'display' => 1 . 'min' );
	return $schedules;
} );

register_activation_hook( __FILE__, function(){
	$interval = 60;
	$action_time = time() + $interval;
	wp_clear_scheduled_hook( 'panpanpan_event' );
	wp_schedule_event( $action_time, 'panpanpan', 'panpanpan_event' );
} );

add_action( 'panpanpan_event', function() {
	$args = array(
		'post_name' => 'test_slug_' . time(),
		'post_date' => date_i18n('Y/m/d'),
		'post_type' => 'post',
		'post_status' => 'pending', 
		'post_title' => 'title', 
		'post_content' => 'content'
	);
	wp_insert_post($args);
} );

wp-includes/post.php line 3165 - 3167. I think that current_user_can function is not operate in wp_cron event.

if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) {
	$post_name = '';
}

Attachments (1)

wp_insert_post.diff (523 bytes) - added by horike 7 years ago.
This patch solve this problem

Download all attachments as: .zip

Change History (3)

@horike
7 years ago

This patch solve this problem

#1 @TobiasBg
7 years ago

The current_user_can function does operate when WP_Cron is run, but the problem is that the cron runs as an unprivileged user -- which does not possess the publish_posts capability.

#2 @ocean90
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Summary changed from A slug can't be registered when it uses wp_insert_post fuinction in wp_cron event to A slug can't be registered when it uses wp_insert_post function in wp_cron event

Duplicate of #19373.

Note: See TracTickets for help on using tickets.