Damon
Damon

Reputation: 135

Update wordpress post to draft if condition is true

I am using the below code to delete the rows from an ACF repeater field with expired date and when the number of rows reaches zero then update the post to draft mode or private mode. But somethings are not woring (The draft thing is not working.)

$ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
$startdate = date("Ymd", strtotime($ap));
$todaydate = date("Ymd");
if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
    $del_data = array(
                    'Ref' => 'sub_seminars_0_ref',
                    'Start date' => 'sub_seminars_0_start_date',
                    'End Date' => 'sub_seminars_0_end_date',
                    'Venue' => 'sub_seminars_0_venue',
                    'Fees' => 'sub_seminars_0_fees',
                    'CPE Credits' => 'sub_seminars_0_cpe_credits'
        );
    delete_row('sub_seminars', 1);
    $row = count( get_field('sub_seminars') );
    if ($row == 0) {
        $postid = $post->ID; //Supply post-Id here $post->ID.
        wp_update_post(array(
            'ID'    =>  $postid,
            'post_status'   =>  'draft'
        ));

    }
}

Please can anybody tell me what is wrong and how to fix it ?

Upvotes: 0

Views: 969

Answers (1)

Damon
Damon

Reputation: 135

 $rows = get_field('sub_seminars');
         $row_count = count($rows);
       // $row_count = count($rows);
        if ($rows == 0) {
           $my_post = array(
               'ID'     => $post->ID,
               'post_status'   =>  'draft'
                );
                wp_update_post($my_post);
            } 

use the above code it fixed the problem for me.

Upvotes: 4

Related Questions