1

I'm currently trying to delete metakeys once my plugin is uninstalled to keep the database clean.

My code is as follows:

Array of meta_keys that are created

$keys = array (
    '_menu_item_icon',
    '_menu_item_hide',
    '_menu_item_block', 
    '_menu_item_center',
}

// Menu items.
        $menu_items = get_posts( array(
            'post_type'   => 'nav_menu_item',
            'numberposts' => -1,
            'nopaging'    => true,
            'fields'      => 'ids'
        ) );


        if ( $menu_items ) {
            foreach ( $menu_items as $post_id ) {
                wp_delete_post( (int) post_id, true );
            }
        }

Was hoping to use an array ('meta_key' => $keys) for the meta_key and iterate through each but that doesn’t seem to work. Is there a better approach?

1
  • Note that you face memory exhaustion and timeout issues here. Consider setting an upper limit of 20-50 posts, and repeatedly running the code over multiple requests. Eventually no posts will be found. Also consider that post meta queries like this are super expensive, you should do this in a WP CLI command at a time when your server has low traffic
    – Tom J Nowell
    Commented Feb 17, 2018 at 0:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.