PHP Warning – Invalid argument in foreach
-
I just installed the CPT UI plugin on http://www.bransonliterature.com/ and noticed a recurring error in the
error_logfile. Here is the error:PHP Warning: Invalid argument supplied for foreach() in /home/.../public_html/wp-content/plugins/custom-post-type-ui/custom-post-type-ui.php on line 340Is this a bug or a conflict? Any ideas?
Thanks
-
This topic was modified 9 years, 7 months ago by
katart17.
-
This topic was modified 9 years, 7 months ago by
-
Hmm, the line in question is this:
foreach ( $post_type['labels'] as $key => $label ) {It’s what populates the user-provided labels for the
register_post_type()function. The only way I can see those parts being messed up in the way the error provide shows, is if somehow something besides an array got saved into the$post_type['labels']index.Is there anything that was done directly to the saved content without using the UI? Perhaps some sort of import that was done from another plugin? Just trying to narrow down possibilities.
I just converted from the Types plugin to CPT UI because of a conflict with WooCommerce. I didn’t import any data. I just created a new Post Type in CPT UI but used the same slug and id so didn’t have to modify the theme files.
Does that make sense?
Makes sense on the surface, but all means, but why it didn’t end up with at least an array of keys that have no values is escaping me at the moment.
For a quick example from the Import/Export page, for one of the post types I just did up:
"foo": { "name": "foo", "label": "Foo", "singular_label": "Foo", "description": "", "public": "true", "publicly_queryable": "true", "show_ui": "true", "show_in_nav_menus": "true", "show_in_rest": "false", "rest_base": "", "has_archive": "false", "has_archive_string": "", "exclude_from_search": "false", "capability_type": "post", "hierarchical": "false", "rewrite": "true", "rewrite_slug": "", "rewrite_withfront": "true", "query_var": "true", "query_var_slug": "", "menu_position": "", "show_in_menu": "true", "show_in_menu_string": "", "menu_icon": "", "supports": ["title", "editor", "thumbnail"], "taxonomies": [], "labels": { "menu_name": "", "all_items": "", "add_new": "", "add_new_item": "", "edit_item": "", "new_item": "", "view_item": "", "search_items": "", "not_found": "", "not_found_in_trash": "", "parent_item_colon": "", "featured_image": "", "set_featured_image": "", "remove_featured_image": "", "use_featured_image": "", "archives": "", "insert_into_item": "", "uploaded_to_this_item": "", "filter_items_list": "", "items_list_navigation": "", "items_list": "" }, "custom_supports": "" }The nested labels array apparently isn’t created for yours. Perhaps try saving the post type settings again?
Thanks Michael, I looked at the Import/Export JSON text and noticed that the labels value was null. I manually added some labels, saved the post type and the labels array has now been recorded. Here is the Exported code:
{ "branch": { "capability_type": "post", "custom_supports": "", "description": null, "exclude_from_search": "false", "has_archive": "false", "has_archive_string": "", "hierarchical": "false", "label": "Branches", "labels": { "add_new": "Add New", "add_new_item": "Add New Branch", "all_items": "All Branches", "edit_item": "Edit Branch", "menu_name": "Branches", "new_item": "New Branch", "not_found": "No Branch Found", "not_found_in_trash": "No Branches found in Trash", "parent": "Parent Branch:", "parent_item_colon": "Parent Branch:", "search_items": "Search Branch", "view_item": "View Branch" }, "menu_icon": "dashicons-building", "menu_position": "", "name": "branch", "public": "true", "publicly_queryable": "true", "query_var": "true", "query_var_slug": "", "rest_base": "", "rewrite": "true", "rewrite_slug": "", "rewrite_withfront": "true", "show_in_menu": "true", "show_in_menu_string": "", "show_in_nav_menus": "false", "show_in_rest": "false", "show_ui": "true", "singular_label": "Branch", "supports": [ "title", "custom-fields", "page-attributes" ], "taxonomies": [] } }I hope this would fix the error
Should have. It now has an array in that spot for when registering. I do find it a bit odd that the order of the settings is different, as all my previous experiences had them pretty consistent. However, it also shouldn’t matter, since we’re not doing indexed arrays.
Did the errors go away after you added the labels?
No errors since. Thanks for the help.
Awesome to hear. Have a good rest of your day.
The topic ‘PHP Warning – Invalid argument in foreach’ is closed to new replies.