Developers: How to bulk update Post Meta data for YARPP

The following function will loop through all posts of a specific post type ("POSTTYPEHERE"), and update the post meta data to match a key with a new value:

add_action('init', 'bulk_update_post_meta_data');

function bulk_update_post_meta_data() {

  // guardrail: if current user can't edit posts, bail
  if ( ! current_user_can( 'edit_posts' ) ) return;
        
  $args = array(
    'posts_per_page' => -1,
    'post_type' => 'POST_TYPE_HERE',
    'suppress_filters' => true
  );

  $posts_array = get_posts( $args );

  foreach($posts_array as $post_array) {
    $yarpp_meta['yarpp_display_for_this_post'] = 1;  // YARPP value to update
    update_post_meta( $post_array->ID, 'yarpp_meta', $yarpp_meta );
  }
}
  • POST_TYPE_HERE - is the post type you are targeting (ex. 'post')
  • $yarpp_meta['yarpp_display_for_this_post'] = 1 to enable Automatic Display Placement.
  • $yarpp_meta['yarpp_display_for_this_post'] = 0 to disable Automatic Display Placement.

This particular code example adds an action to "init", meaning it will load immediately – so don't forget to remove it when you're done!  Load any page on your website once – then remove or comment this code out thereafter from your theme's functions.php file as there is no need to keep this function active as it will otherwise continue to run on every page load for anyone that has permission to edit posts. 

Was this article helpful?
1 out of 1 found this helpful