0

I am designing a website intended to list local events. Ideally registered users would be able to upload new events from the front end and the website would display all the upcoming events in chronological order.

I have registered an "Event" custom post type, as a first step, with several custom fields (category, date, time, etc.).

Now I am creating a cron job that loops through the events hourly and removes the ones that have expired.

Finding the events to remove is an easy task.

1) The cron job gets the current date, firstly:

$currentdate= date("Y-m-d");

2) It then gets the starting date of the event (stored in a custom field):

$eventdate = get_field('date');

3) And compares the two with a simple conditional statement:

if ($currentdate > $eventdate ) do something

At this point I would like to make it so that the cron job changes the post type of the expired events to a completely different one - I reckon it would be the best approach (the amount of active events wouldn't progressively increase with time, this way).

My question is then: is there a way to tell Wordpress to change the post type of a post into another?

Thanks in advance for your help :3

2

You can use Function Reference/set post type

<?php
$post_id = 15;
if ( set_post_type( $post_id, 'page'  ) ) {
  echo "Post #$post_id is now a Page";
} else {
  echo "Impossible to transform this post into a page";
}
?>
1
  • damn, had no idea such a function existed! truly appreciated :3 Jul 3 '19 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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