Add Category After Date From ACF Date Picker Field Has Expired
Need to add category to custom post type entries after a date from an acf date picker field has passed.
I found the following code which almost does the same thing. The difference is that it sets the expired post to draft instead of adding a category.
// Expire events
if ($expireTransient = get_transient($post-ID) === false) {
set_transient($post-ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
$today = date('Ymd', current_time('timestamp', 0));
$args = array(
'post_type' = 'ausstellung',
'posts_per_page' = 200,
'post_status' = 'publish',
'meta_query' = array(
array(
'key' = 'ende_datum',
'value' = $today,
'compare' = '='
)
)
);
$posts = get_posts($args);
foreach( $posts as $post ) {
if(get_field('ende_datum', $post-ID)) {
$postdata = array(
'ID' = $post-ID,
'post_status' = 'draft',
);
wp_update_post($postdata);
}
}
}
This is how i changed the code – unfortunately it isn't working.
// Expire events
if ($expireTransient = get_transient($post-ID) === false) {
set_transient($post-ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
$today = date('Ymd', current_time('timestamp', 0));
$args = array(
'post_type' = 'ausstellung',
'posts_per_page' = 200,
'post_status' = 'publish',
'meta_query' = array(
array(
'key' = 'ende_datum',
'value' = $today,
'compare' = '='
)
)
);
$posts = get_posts($args);
foreach( $posts as $post ) {
if(get_field('ende_datum', $post-ID)) {
$postdata = array( 'ID' = $post-ID, );
$cat_ids = array( 32 );
wp_set_object_terms($postdata, $cat_ids, 'hf_cat_ausstellung');
}
}
}
The custom taxonomy is named hf_cat_ausstellung and is registered trough a plugin called happy files, the id of the term called past is 32.
Topic advanced-custom-fields date datepicker Wordpress
Category Web