Add Category After Date Expired
-
I want to add a term with an id of
32
of the custom taxonomyhf_cat_ausstellung
to posts of a custom post type when a date from acf date picker field has passed.I found the following code snippet but can’t get it to work.
// 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'); } } }
my custom post type is called
ausstellung
and the custom field has the keyende_datum
. I adapted the code from this one here:// 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); } } }
The above code is working but sets the posts to drafts instead of adding a category.
I’m a wordpress beginner so maybe anybody has a hint what is wrong with my code.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Add Category After Date Expired’ is closed to new replies.