List All Custom Post Types Posts in Nav Menu Dynamically

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!

Read more here:: List All Custom Post Types Posts in Nav Menu Dynamically

Leave a Reply

Your email address will not be published. Required fields are marked *