wp_get_associated_nav_menu_items

The timeline below displays how wordpress function wp_get_associated_nav_menu_items has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Returns the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int    $object_id   Optional. The ID of the original object. Default 0.
 * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'.
 *                            Default 'post_type'.
 * @param string $taxonomy    Optional. If $object_type is 'taxonomy', $taxonomy is the name
 *                            of the tax that $object_id belongs to. Default empty.
 * @return int[] The array of menu item IDs; empty array if none.
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' === $object_type && 'post_type' === $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' === $object_type && 'taxonomy' === $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 5.6

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int    $object_id   Optional. The ID of the original object. Default 0.
 * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'.
 *                            Default 'post_type'.
 * @param string $taxonomy    Optional. If $object_type is 'taxonomy', $taxonomy is the name
 *                            of the tax that $object_id belongs to. Default empty.
 * @return int[] The array of menu item IDs; empty array if none.
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' === $object_type && 'post_type' === $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' === $object_type && 'taxonomy' === $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 5.5

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int    $object_id   The ID of the original object.
 * @param string $object_type The type of object, such as 'taxonomy' or 'post_type'.
 * @param string $taxonomy    If $object_type is 'taxonomy', $taxonomy is the name of the tax
 *                            that $object_id belongs to.
 * @return int[] The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' === $object_type && 'post_type' === $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' === $object_type && 'taxonomy' === $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 5.4

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int    $object_id   The ID of the original object.
 * @param string $object_type The type of object, such as 'taxonomy' or 'post_type'.
 * @param string $taxonomy    If $object_type is 'taxonomy', $taxonomy is the name of the tax
 *                            that $object_id belongs to.
 * @return int[] The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' == $object_type && 'post_type' == $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' == $object_type && 'taxonomy' == $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 4.3

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int    $object_id   The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @param string $taxonomy    If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' == $object_type && 'post_type' == $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' == $object_type && 'taxonomy' == $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 4.2

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int $object_id The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' == $object_type && 'post_type' == $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } elseif ('taxonomy' == $object_type && 'taxonomy' == $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 3.9

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int $object_id The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' == $object_type && 'post_type' == $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } else if ('taxonomy' == $object_type && 'taxonomy' == $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                $menu_item_ids[] = (int) $menu_item->ID;
            }
        }
    }
    return array_unique($menu_item_ids);
}

WordPress Version: 3.7

/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int $object_id The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $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 || get_post_meta($menu_item->ID, '_menu_item_object', true) !== $taxonomy) {
                continue;
            }
            $menu_item_ids[] = (int) $menu_item->ID;
        }
    }
    return array_unique($menu_item_ids);
}