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' );
}
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' );
}
Share
Improve this question
edited Apr 21, 2019 at 22:22
Delek Smythe
asked Apr 21, 2019 at 22:09
Delek SmytheDelek Smythe
113 bronze badges
1 Answer
Reset to default 1Your 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' );
}