How do I fix this error: Warning: invalid argument supplied for foreach()?

This usually happens when you are trying to do admin navigation stuff and you aren’t hooked into admin_menu. If you hook in before that, $menu hasn’t been created yet.

add_action('admin_menu', 'my_plugin_add_menu');

function my_plugin_add_menu(){
    $ptype="my_post_type";
    $ptype_obj = get_post_type_object( $ptype );
    add_submenu_page( 'my-menu-item', $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
}

Leave a Comment