I am trying to get the post id by the navigation id to use in a loop. I made a new query to get posts. I am using multiple queries on a page because I need to. one of the queries gets page templates that have categories that you make a post to and it gets displayed by that template. My problem is that if I put in the query to get posts, then I end up with the data in the theme twice. So I want to make it so that only a post that has a navigation link will be displayed. It will still be able to link to the post but I only want the query to get a post that has a nav item. Every time I try, all I end up with is a bunch of this


The functions I have read about, some from this site aren't working the way I understand they should. I have taken code straight out of the codex and it comes up empty http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

                $menu_name = 'primary';

if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

$menu_items = wp_get_nav_menu_items($menu->term_id);
echo $menu_items->ID;

My attempts have returned nothing.

1 Answer 1


I think you need to loop the $menu_items because the function, according to the Codex, returns a array.

Remplace echo $menu_items->ID; with

 foreach ( $menu_items as $menu_item ) {
    echo $menu_item->ID;

I need to check the array exactly, but if you need to compare a post ID to a menu item, you will need to compare $menu_item->object_id which contains the ID of the post the menu-item is linking to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.