Assign different taxonomy term to custom post type based on field value
-
I have a custom post type ‘road_trip’ with taxonomy ‘tripstatus’ terms ‘upcomingadventures’ (id 10) and ‘completedadventures’ (id 11).
I already added to functions.php a function to assign all new ‘road_trip’ posts automatically to the ‘upcomingadventures’ term using this:
`function add_tripcategory_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
$tripcat = array (10);
wp_set_object_terms( $post_ID, $tripcat, ‘tripstatus’);
}
}
add_action(‘publish_road_trip’, ‘add_tripcategory_automatically’);`All ‘road_trip’ posts have a field ‘end_date’ (the end date of a road trip, in the future at the time of publishing a post). How can we automatically move posts from ‘upcomingadventures’ to ‘completedadventures’ when the timestamp of ‘end_date’ is less than the timestamp of ‘now’? Hook into wp_init or wp_loaded? Adding to functions.php seems like the right approach but the part about needing to do it to all posts with term ‘upcomingadventures’ is throwing me off.
I am thinking a wp query and a loop with the timestamp conditional inside it
if strtotime(get_post_meta(get_the_id(), 'start_date', true)) > strtotime('now')
and then a wp_set_object_terms() to move the post to ‘completedadventures’ when ‘now’ is later than ‘end_date’ would work?
Not sure about the syntax details for the loop (or other approach) to call up all ‘upcomingadventures’ road trips to be tested for ‘end_date’ and then, where applicable, moved to the ‘completedadventures’ term.
- The topic ‘Assign different taxonomy term to custom post type based on field value’ is closed to new replies.