0

Ive successfully added all posts (of a custom post type) to my wordpress menu using this method: https://codeseekah.com/2012/03/05/list-all-posts-in-wordpress-navigation-menu/

Everything is working great. Unfortunately I get this error when saving the menu in wp admin: "The given object ID is not that of a menu item." I get a long list of like 20 of them (one error for each menu item).

This is what I have in my functions file:

/**
 * List all posts in wp nav menu
 * @link https://codeseekah.com/2012/03/05/list-all-posts-in-wordpress-navigation-menu/
 * @param  [type] $items [description]
 * @param  [type] $menu  [description]
 * @param  [type] $args  [description]
 * @return [type]        [description]
 */
/* take care of the urls */
add_filter( 'wp_get_nav_menu_items', 'services_menu_filter', 12, 3 );
function services_menu_filter( $items, $menu, $args ) {
    /* alter the URL for cpt-archive objects */

    $menu_order = count($items); /* Offset menu order */
    $child_items = array();

    foreach ( $items as &$item ) {
        if ( $item->title != '##services##' ) continue;
        $item->url = '/services/';
        $item->title = 'Services';

        foreach ( get_posts( 'post_type=services&numberposts=-1' ) as $post ) {
            $post->menu_item_parent = $item->ID;
            $post->post_type = 'nav_menu_item';
            $post->object = 'custom';
            $post->type = 'custom';
            $post->menu_order = ++$menu_order;
            $post->title = $post->post_title;
            $post->url = get_permalink( $post->ID );
            /* add children */
            $child_items []= $post;
        }
    } 
    return array_merge( $items, $child_items );
}

Any ideas to get rid of the wordpress menu error notifications? Everything works fine and I could live with it, but i dont want to throw off the client if/and when they see all the errors. Yes, I thought about hiding the errors with CSS, but thats just not good enough.

The web host is Kinsta. I've tried adding the following to wp-config.php:

/** Memory Limit */
define('WP_MEMORY_LIMIT', '128M');

Thanks!

1

I think you're only adding the custom posts to the nav menu on the front-end and not on the back-end/wp-admin.

Therefore, you can use is_admin() to see if the hook wp_get_nav_menu_items is being called on the front-end or from an "admin" page such as the Appearance → Menus page.

So, replace this:

function services_menu_filter( $items, $menu, $args ) {
    /* alter the URL for cpt-archive objects */

..with this one:

function services_menu_filter( $items, $menu, $args ) {
    if ( is_admin() ) {
        return $items;
    }

    /* alter the URL for cpt-archive objects */

Alternatively, replace this:

add_filter( 'wp_get_nav_menu_items', 'services_menu_filter', 12, 3 );

..with this one:

if ( ! is_admin() ) {
    add_filter( 'wp_get_nav_menu_items', 'services_menu_filter', 12, 3 );
}
| improve this answer | |
  • 1
    That was it. I had to add this line right inside the function. if ( is_admin() ) { return $items; } Thank you. – mattbru Apr 10 '18 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.