• Resolved tiaanswart

    (@tiaanswart)


    Hi

    This post is here to help anyone who is maybe looking at a way to get the menu item id by using the menu item name.

    I was busy developing a theme when I needed a function to give me the menu item id of a menu item I created so that I can link another menu item to the new menu item as a child.

    Hope this makes some sense…

    So what I did was I created a function that was hooked in to the after_setup_theme action, this function created some default pages and posts to demonstrate the capabilities of the theme.

    After the pages where created successfully a function will then be run to create a new custom menu with links to the newly created pages.

    My issue became clear that when I created the menu I did not have the ability to create parent menu items with linked child menu items…

    So I created the below function that gets me the menu item id of a specified menu item (given you know the name of the item) so that I can use it again when creating a child menu item in the wp_update_nav_menu_item function in the menu-item-parent-id variable within the parameters array…

    /*******************************
     Get Menu Item ID
     @since: v1.0
     @author: Tiaan Swart
     @params:
        $menu_id - The menu ID in which the item you are looking for is located
        $item_name - The name of the item which you are looking for
        $item_type - The type of menu item (ie. page, post, custom), the default is custom
    ********************************/
    function ts_get_menu_item_id( $menu_id, $item_name, $item_type = 'custom' ) {
    
        //If there is no Menu ID or Item Name specified return false
        if ( empty($menu_id) ) {
            return FALSE;
        } elseif ( empty($item_name) ) {
            return FALSE;
        }
    
        //If the Item Type specified is not a normal item type change it to custom
        if ( in_array( $item_type, array( 'custom', 'page', 'post', 'default' ) ) ) {
    		$item_type = $item_type;
    	} else {
    		$item_type = 'custom';
    	}
    
        //Get the global wordpress database class
        global $wpdb;
    
        //build the query we are going to use to extract the id from the database
        //we only need the ID of the menu item, which is the ID of the nav_menu_item in the wp_posts table
        $query = 'SELECT ID ';
        //we are going to work with data from 3 tables wp_posts, wp_term_relationships and wp_postmeta lets add them to the mix
        $query .= 'FROM '.$wpdb->posts.', '.$wpdb->term_relationships.', '.$wpdb->postmeta.' ';
        //now lets specify the conditions
        $query .= 'WHERE ';
        //we need to refine our query to only show rows from the 3 tables where the ID's match
        $query .= 'ID = object_id AND ID = post_id ';
        //we also only want menu items from the given menu id
        $query .= 'AND term_taxonomy_id = "'.$menu_id.'" ';
        //we also only want menu items matching the given item name
        $query .= 'AND post_title = "'.$item_name.'" ';
        //return rows from wp_posts that are published only
        $query .= 'AND post_status = "publish" ';
        //return rows from wp_posts that are menu items only
        $query .= 'AND post_type = "nav_menu_item" ';
        //return rows from wp_postmeta where the meta key is menu item object
        $query .= 'AND meta_key = "_menu_item_object" ';
        //return rows from wp_postmeta where the meta value for menu item object matches the given item type
        $query .= 'AND meta_value = "'.$item_type.'" ';
        //the query is now complete
    
        //lets run the query and return the result as a variable
    
        return $wpdb->get_var( $query );
    }

    If anyone has any improvements on this function please do share…

    Also I just have to thank the users of this post as they were the inspiration…

  • The topic ‘Get the menu item ID by using menu item NAME’ is closed to new replies.