WordPress allows users to create custom menus to organize their website’s navigation. These custom menus can be created and managed through the Appearance > Menus section in the WordPress dashboard. However, sometimes, it may be necessary to query these custom menus in order to display them on the front-end of the website.

One way to query custom menus in WordPress is through the query_posts() function. This function allows users to retrieve specific posts or pages based on certain parameters, such as post type, taxonomy, and order.

    $args = array(
    'post_type' => 'nav_menu_item', 'orderby'   => 'menu_order',
    'order'  => 'ASC',
    'tax_query' => array(
            'taxonomy' => 'nav_menu', 'field' => 'slug', 'terms' => 'my'        
while( have_posts()):the_post();
echo $post->ID;
endwhile; wp_reset_query();?>

The code snippet provided above demonstrates how to query a custom menu in WordPress using the query_posts() function. The first step is to define an array of arguments for the function. In this example, we are setting the post type to “nav_menu_item” and ordering the results by “menu_order” in ascending order.

The next step is to define a taxonomy query, which is used to retrieve the specific custom menu we want to query. In this case, we are using the taxonomy “nav_menu” and specifying the field as “slug” and the term as “my”. This means that we are querying the custom menu with the slug “my”.

Once the arguments are defined, we can pass them to the query_posts() function and start looping through the results. In this example, we are using the while loop and the have_posts() function to iterate through the results, and we are also displaying the post ID of each menu item.

Finally, it is important to reset the query after the loop is finished by using the wp_reset_query() function. This ensures that the query does not affect any other queries on the website.


In conclusion, querying custom menus in WordPress can be useful when displaying specific navigation items on the front-end of a website. By using the query_posts function and passing in the appropriate arguments, we can target specific menus by their slug and display them in any order we choose. In the example above, we are querying the ‘nav_menu_item’ post type, ordering the results by ‘menu_order’, and displaying items from the ‘my’ menu.

It’s important to note that the query_posts function is considered a legacy function in WordPress and should be used with caution. It’s recommended to use the WP_Query class or get_posts() instead.

One of the advantages of using this method is that it allows you to display specific menu items outside of their designated menu location. For example, if you wanted to display specific items on the homepage or in a sidebar, you can use the query_posts function to target those specific menu items and display them in any layout you choose.

Additionally, this method is also useful for displaying specific items in a mobile menu or in a dropdown menu. You can target specific items and only show them in certain scenarios, providing a more tailored experience for your users.

It’s important to remember to use the wp_reset_query() function after the loop to restore the original query.

By using the query_posts function and the appropriate arguments, we can easily target and display specific custom menus in WordPress. This can provide a more tailored and dynamic experience for our users, and give us more control over the navigation of our website.