2

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

1 Answer 1

0

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

4
  • Yes, that is exactly what I meant to write.
    – Mathias
    Jun 8, 2018 at 8:47
  • See my edit....
    – Bjorn
    Jun 8, 2018 at 9:18
  • Concise and precise :-) I'm still new to php so I need some clarifying before I can apply the code. What do I need to finish $concert_start_date_str collection? Next, the site is still local, so might I'll be using a plugin for the cron-job - how will the code look with a hook?
    – Mathias
    Jun 9, 2018 at 18:59
  • Well, this is not a site were we hold your hand every step of the way ;-). You have to get koncerter_start_date from the location it's saved. How, is up to you. Don't use a plugin for the cron-job, create one from your hosting panel. You can place this code directly in functions.php. Set the correct domain in the first function and try it. The hooks are already inside the add_action(). For all your other questions i suggest Google ;-).
    – Bjorn
    Jun 10, 2018 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.