wp_get_associated_nav_menu_items

Definition:
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type' ) {}

Get the menu items associated with a particular object.

Parameters

  • int $object_id: The ID of the original object.
  • string $object_type: The type of object, such as “taxonomy” or “post_type.”

Return values

returns:The array of menu item IDs; empty array if none;

Source code

function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type' ) {

	$object_id = (int) $object_id;

	$menu_item_ids = array();



	$query = new WP_Query;

	$menu_items = $query->query(

		array(

			'meta_key' => '_menu_item_object_id',

			'meta_value' => $object_id,

			'post_status' => 'any',

			'post_type' => 'nav_menu_item',

			'posts_per_page' => -1,

		)

	);

	foreach( (array) $menu_items as $menu_item ) {

		if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {

			if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) != $object_type )

				continue;



			$menu_item_ids[] = (int) $menu_item->ID;

		}

	}



	return array_unique( $menu_item_ids );

}

3679

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: