-
-
Save daggerhart/c17bdc51662be5a588c9 to your computer and use it in GitHub Desktop.
When adding posts or taxonomy terms with add_object
, this notice will happen for the currently active menu item:
Notice: Undefined property: stdClass::$post_parent in .../wp-includes/nav-menu-template.php on line 415
To avoid this, we must add a post_parent
property to the $item_obj
object on the make_item_obj
method.
From the tests I've done adding both posts and taxonomy terms via the regular Appearance > Menus tool at wp-admin, I've come to the conclusion that post_parent
is the actual post_parent
for posts and the term parent
for taxonomy terms.
So this is what needs to be done:
-
At
add_object
: Addpost_parent
as$object->post_parent
to the menu_item when$object_type == 'post'
-
At
add_object
: Add tt.parent to the SQL query atadd_object
when$object_type == 'term'
-
At
add_object
: Addpost_parent
as$object->parent
to the menu_item when$object_type == 'term'
-
At
make_item_obj
: Add$item_obj->post_parent
as!empty( $item['post_parent'] ) ? $item['post_parent'] : ''
You can find the full class with the changes at https://gist.github.com/webdados/e069ce33947a0e981aede33c79a4d837
Thanks for digging into that @webdados!
Those changes make great sense. I've updated this gist to reflect the changes. Very much appreciated!
You're welcome @daggerhart
I added "classes" support here: https://gist.github.com/webdados/d7a31f1363ef65d8a220e5ec87c1f1e8
Moved to this repo. PRs welcome! https://github.com/daggerhart/wp-custom-menu-items
Nice!
@codepuncher - thanks for the updated method, I've added it to the gist.
@vandelio - I've added a new entry point for adding Posts and Taxonomy terms to the gist, and updated the example with its usage. See
custom_menu_items::add_object()