1

I'm attempting to regularly update a custom field ($company_rating) in a CPT with update_post_meta(). The cronjob is assigned and working. I've tested the function and it prints correctly (I've shortened it here for readability). But the fields are not updating.. Any ideas?

function update_company_ratings_test() {    
    global $post;

    $comp_args = array( 
        'post_type'=>'boat-company',
        'posts_per_page'=>'-1',
        'post_status '=> 'publish',

    );

    $posts_array = get_posts( $comp_args );

    if ($posts_array) {
        foreach( $posts_array as $post_id ){

            setup_postdata($post);

            $company_id = $post_id->ID;

            $company_rating = 42;

            update_post_meta( $company_id, 'company_rating', $company_rating, true );
        }
    }

    wp_reset_postdata();

}

add_action( 'update_cron_test', 'update_company_ratings_test' );

if( !wp_next_scheduled( 'update_cron_test' ) ) {  
    wp_schedule_event( time(), 'hourly', 'update_cron_test' );  
}  

1 Answer 1

1

Your problem is caused by your code... Let me comment it line by line, so the problem is more visible...

global $post;  // <- great, you use global $post variable

$comp_args = array( // <- you set params for your query
    'post_type'=>'boat-company',
    'posts_per_page'=>'-1',
    'post_status '=> 'publish',

);

$posts_array = get_posts( $comp_args );  // <- you run the query

if ($posts_array) {  // you check if there are any posts
    foreach( $posts_array as $post_id ) { // and if there are any posts, you loop through them and (but not in a correct way)...

        // but this (next) line doesn't do anything... The global $post variable won't be set
        // during Cron call... So this line won't work as you want it to...
        setup_postdata($post);

        // ... and that means that you can't get ID from $post, so $company_id won't contain any ID
        $company_id = $post_id->ID;

        $company_rating = 42;

        // so this line won't work as well...
        // also it would change only those meta fields that were containing "true", so...
        update_post_meta( $company_id, 'company_rating', $company_rating, true );
    }
}

wp_reset_postdata();  // you don't have to reset query in here, because you haven't changed any global query...

So here's the cleaner way to do what you want:

function update_company_ratings_test() {    
    $comp_args = array( 
        'post_type'=>'boat-company',
        'posts_per_page'=>'-1',
        'post_status '=> 'publish',    
        'fields' => 'ids'
    );

    $posts_array = get_posts( $comp_args );

    foreach( (array)$posts_array as $company ) {    
        $company_rating = 42;
        update_post_meta( $company->ID, 'company_rating', $company_rating );
    }
}
add_action( 'update_cron_test', 'update_company_ratings_test' );

if ( !wp_next_scheduled( 'update_cron_test' ) ) {  
    wp_schedule_event( time(), 'hourly', 'update_cron_test' );  
}  
2
  • Thank you Krzysiek, unfortunately that isn't working either. Any other ideas? Apr 28, 2019 at 22:34
  • @DelekSmythe so it’s not a WP problem any more, I guess... The code above should work just fine as long, as the names of CPT (and so on) are correct. Apr 29, 2019 at 4:48

Your Answer

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

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