Support

Account

Home Forums General Issues Programmatically clear field values for all posts? Reply To: Programmatically clear field values for all posts?

  • There isn’t any such thing as a “run once ever” mechanism in WP.

    In addition to this, if you have many posts then looping over all of them to delete the field will more then likely time out.

    This is the way I deal with this.

    
    add_action('init', 'MY_FUNCTION_NAME');
    function MY_FUNCTION_NAME() {
      if (!is_user_logged_in() || get_current_user_id() != XX) {
        // replace XX with your user ID
        // only run this for me when I am logged in
        return;
      }
      // query all posts
      // use your field name in meta query
      // only get posts where the meta_key "EXISTS"
      // loop over results and delete_field() on each
    }
    

    If you have a lot of posts then this will time out the page load. If this happens reload the page until it not longer does so. This will indicate that the field has been deleted from all posts.