admin管理员组

文章数量:1421223

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

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

Share Improve this question asked Jul 3, 2019 at 16:54 Campioni Del MondoCampioni Del Mondo 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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";
}
?>

本文标签: wp cronConvert post type to another