Changing post_status in wordpress

I’m trying to programatically change the post status of a post being created from the front end of a website.

The user fills out a form and gets a preview of their post, if they’re happy with it, they hit a submit button which should get the ID, and change the post_status to either publish or pending, depending on whether or not they are a pre-approved user.

Read More

The code I have is:

if( isset($_POST['publish-event']) ) :

    // check if they have an approved event
    global $current_user;
    get_currentuserinfo();

    $user_id = $current_user->ID;

    if( get_field('approved_organiser', 'user_'.$user_id.'') == true ) :
        $approved = true;
    endif;  


    if( $approved == true ) :
        $status = 'publish';
    else :
        $status = 'pending';
    endif;


    // update event based on status
    $update_post = array(
        'post_type' => 'event',
        'ID' => $_POST['id'],
        'post_status' => $status,
        'edit_date' => true,
        'post_date' => $_POST['post_date']
    );

    var_dump($update_post);
    wp_update_post($update_post);


    $eventCreated = true;

endif; // end if publish

However, the event remains a draft in WordPress despite the $update_post variable var_dumping as saying pending in this case.

Any ideas why it isn’t changing the post status?

————————————————————————————-
[EDIT]

Removing the date information from the update_post lets it save as pending. I’ve just tried splitting it up so I do the status first, then the date separately, but this set the status to pending and didn’t change the post date to the selected scheduled time.

Code:

$update_status = array(
    'post_type' => 'event',
    'ID' => $_POST['id'],
    'post_status' => $status
);
$statusTest = wp_update_post($update_status);
var_dump($statusTest);


$update_date = array(
    'post_type' => 'event',
    'ID' => $_POST['id'],
    'post_date' => $_POST['post_date']
);
$dateTest = wp_update_post($update_date);
var_dump($dateTest);

In both cases the var_dump spits out the ID of the event, instead of 1 though. I’m not sure why.

————————————————————————————-
[EDIT 2]

I’ve just been googling around and apparently someone had success with adding 'post_date_gmt' => $your_post_date to the array also, this still didn’t work for me though.

Related posts

Leave a Reply

1 comment

  1. Managed to solve this by using post_date_gmt, however I had to split the update up into two parts to actually get it to work. I have the following code that now works:

    $update_post = array(
        'post_type' => 'event',
        'ID' => $_POST['id'],
        'post_status' => $status
    );
    
    $statusTest = wp_update_post($update_post);
    
    
    if( $_POST['post_date'] > date('Y-m-d H:i:s') ) :
    
        $gmtdate = gmdate( 'Y-m-d H:i:s', $_POST['post_date'] );
    
        $update_post = array(
            'post_type' => 'event',
            'ID' => $_POST['id'],
            'edit_date' => true,
            'post_date' => $_POST['post_date'],
            'post_date_gmt' => $gmtdate
        );
    
        $dateTest = wp_update_post($update_post);
    
    endif;
    

    I’m not exactly sure why this works, if I move the code from the second wp_update_post into the first one, it fails. Formatted like this, it works. Hopefully this helps a few people out as it seems like an issue that keeps cropping up every now and then.