wp_set_object_terms in loop is not work in taxonomy & CPT
-
I have developed a query to set object terms for CPT after some days. The code is placed in the functions.php/child-theme.
The code is given below.function set_expired_job_categories() { global $post; $current_time = time(); $job_status = 'current-status'; $job_expired_id = 368; $job_ongoing_id = 367; // Set our query arguments $args = array( 'fields' => 'ids', // Only get post ID's to improve performance 'post_type' => 'job', // Post type 'post_status' => 'publish', 'posts_per_page' => -1, 'tax_query' => array( 'taxonomy' => 'current-status', 'field' => 'slug', 'terms' => array( 'ongoing' ), ), ); $job_expiration_query = new WP_Query( $args ); // Check if we have posts to delete, if not, return false if( $job_expiration_query->have_posts() ){ while( $job_expiration_query->have_posts() ){ $job_expiration_query->the_post(); $postid = get_the_ID(); $expire_timestamp = rwmb_meta( 'deadline_date' ); if ( $expire_timestamp ) { $seconds_between = ( (int)$expire_timestamp - (int)$current_time ); if ( $seconds_between >= 0 ) { // for debugging only echo $postid . ' . ' .$seconds_between . 'is greater than 0' . $expire_timestamp .'<br>'; }else { //for debugging only echo $seconds_between . 'is less than 0' . '<br>'; wp_set_object_terms( $postid, $job_expired_idd, $job_status, true ); wp_remove_object_terms( $postid, $job_ongoing_id, $job_status ); } } } wp_reset_postdata(); } } // expired_post_delete hook fires when the Cron is executed add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );
In the above code, the query executes all the other things except the function ‘wp_set_object_terms’. But, if I add the “init” hook for this function, it set the terms. But “init” hook consumes a lot of resources when action fires.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘wp_set_object_terms in loop is not work in taxonomy & CPT’ is closed to new replies.