1 day after custom date change post status to draft

1 day after koncerter_start_date all posts with custom-post-type koncert have to get the status draft

1 Answer
1

Your question isn’t very clear.

Do you mean:

1 day after koncerter_start_date all posts with custom-post-type koncert have to get the status draft?


EDIT

Code:

add_action( 'wp_loaded', 'concert_daily_tasks' );
function concert_daily_tasks() {
  $current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  if ( $current_url == 'https://example.com/concert-daily-tasks' ) { // <-- set the correct URL!
    check_concert_status();
  }
}

function check_concert_status() {
  $today = new DateTime();

  $concert_start_date_str="2018-10-10"; // I'm not sure where/how you get this data.
  $concert_start_date = DateTime::createFromFormat('Y-m-d', $concert_start_date_str); // make sure the format ('Y-m-d') matches $concert_start_date_str

  if($concert_start_date) {
    if($concert_start_date->modify('+1 day') <= $today) { // if concert start day was yesterday (or older) continue
      $args = array(
        'posts_per_page'   => -1, // get all posts
        'post_type'        => 'koncert',
        'post_status'      => 'publish',
      );
      $posts = get_posts($args);
      if($posts) {
        foreach($posts as $post) {
          wp_update_post(array(
            'ID' => $post->ID,
            'post_status' => 'draft',
          ));
        }
      }
    }
  }
}

Important:

  • Finish $concert_start_date_str collection, see script.
  • You can place this in functions.php
  • I’ve included a cron-job listner, when you go to
    https://example.com/concert-daily-tasks the code will run. You probable can setup a
    cron-job schedule in your hosting panel.
  • The cron-job will return a 404, but the code will still run, i didn’t
    include a 200 OK response, you can do this yourself if you want it ;-).

Greetz, Bjorn

Leave a Comment