是的,您可以安排一个WP cron作业来循环浏览一个帖子类型的所有帖子,并执行检查/添加术语。但对于一个有数千篇帖子的大页面,我不建议这样做。
在下面的示例中,计划的作业大约每天运行一次-请记住,wp cron不是很准确,它只在您的站点被访问时计划下一个任务(请参阅this question)
// Scheduled job callback
function run_update_tax_cron_job( ) {
$posts = get_posts( [
\'post_type\' => \'your-post-type-slug\',
\'posts_per_page\' => -1, // getting all posts of a post type
\'no_found_rows\' => true, //speeds up a query significantly and can be set to \'true\' if we don\'t use pagination
\'fields\' => \'ids\', //again, for performance
] );
//now check meta and update taxonomy for every post
foreach( $posts as $post_id ){
$meta_value = get_post_meta( $post->ID, \'new_used_cat\', true );
$new_term = ! empty( $meta_value ) ? \'used\' : \'new\';
wp_set_object_terms( $post_id, $new_term ,\'vehicle_condition\',
false);
}
}
// Schedule Cron Job Event
function udpdate_taxonomy_cron_job() {
if ( ! wp_next_scheduled( \'run_update_tax_cron_job\' ) ) {
wp_schedule_event( current_time( \'timestamp\' ), \'daily\', \'run_update_tax_cron_job\' );
}
}
add_action( \'wp\', \'udpdate_taxonomy_cron_job\' );