Skip to content

Instantly share code, notes, and snippets.

@lichtmetzger
Last active August 9, 2022 12:58
Show Gist options
  • Save lichtmetzger/43781de1b4a7bd5781c02307a78247af to your computer and use it in GitHub Desktop.
Save lichtmetzger/43781de1b4a7bd5781c02307a78247af to your computer and use it in GitHub Desktop.
Update meta fields for CPT in WordPress if not set already
<?php
$query = new WP_Query(array(
'post_type' => 'company',
'posts_per_page' => -1
));
$posts = [];
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$posts[] = $post_id;
}
wp_reset_query();
foreach($posts as $post_id) {
echo $post_id;
if (get_post_meta($post_id, 'ctl_visibility', true) === 'true') {
// Do nothing
} else {
update_post_meta($post_id, 'ctl_visibility', 'false');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment