0

I have to change the published post in one category to another category after some days. I have created the below code for the function, which I have added in the functions.php file in child-theme. But, I don't know the error piece. The below data display the list of post based on the calculation in the loop. the only missing function is wp_set_object_terms. Please help me out with this.

function set_expired_job_categories() {

    $current_time = time();// define timestamp

    // 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' => 10,
        'tax_query' => array(
            'taxonomy' => 'current-status',
            'field' => 'slug',
            'terms' => array( 'ongoing' ),
        ),
    );
     $q = new WP_Query( $args ); //new wp_query

    // Check if we have posts, if not, return false
    if ( !$q )
        return false;

    // standard WP_query
   if( $q->have_posts() ){
        while( $q->have_posts() ){
            $q->the_post();

            //deadline_date saved as timestamp
            $expire_timestamp = rwmb_meta( 'deadline_date' );
            if ( $expire_timestamp ) {
                $seconds_between = ( (int)$expire_timestamp - (int)$current_time );
                if ( $seconds_between >= 0 ) {
                }else {
                    wp_set_object_terms( $post->ID, array ( 368 ), 'current-status', true );
                    wp_remove_object_terms( $post->ID, array ( 367 ), 'current-status' );
                }
            }
        }
    }
    wp_reset_postdata();
}

// hook fires when the Cron is executed
add_action( 'set_job_categories', 'set_expired_job_categories' );

// Add function to register event to wp
add_action( 'wp', 'register_daily_events_job_expiration');

function register_daily_events_job_expiration() {
    // Make sure this event hasn't been scheduled
    if( !wp_next_scheduled( 'set_job_categories' ) ) {
        // Schedule the event
        wp_schedule_event( time(), 'hourly', 'set_job_categories' );
    }
}

I have revised as follows

$taxonomy = 'current-status';
$job_expired_id = 368;
$job_ongoing_id = 367;

$postid = get_the_ID();
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );

For this also, no result.

1 Answer 1

0

As it is in the functions file, I need to set the global $wp_taxonomies to populate the taxonomies data initially. Also, instead of using the tag_ID, I have revised with the slug. These two changes helped to work out the code. The revised code is below for reference. Thank you all for the efforts.

function set_expired_job_categories() {
    
    global $post;
    global $wp_taxonomies;

    $current_time = time();
    $taxonomy = 'current-status';
    $job_expired_id = 'expired';
    $job_ongoing_id = 'ongoing';

    // 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 set categories, 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 ) {
                }else {
                    wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
                    wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
                }
            }
        }
        wp_reset_postdata();
    }
}

// hook it to low priority value, due to CPT and Taxonomies
add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );

Reference: https://wordpress.org/support/topic/wp_set_object_terms-in-loop-is-not-work-in-taxonomy-cpt/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.