For custom post type "bedrijf", i created a possibility where visitors can leave a rating. If they do, the meta key and value is added and saved. If there is more than one, the average is calculated and the value field is updated.
However, most custom posts don't have a meta key and value yet and i want them to have one with value "0".
The code I got so far:
function create_metadata(){
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => -1,
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
$av = get_post_meta($post->ID, 'averagerating', true);
if (!isset($av)){
add_post_meta( $post->ID, 'averagerating', '0' );
}
}
}
add_action('init','create_metadata');
But so far this didn't work.