Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] A function used to retrieve all of the post IDs that are being represented by a single menu.
<?php
function acme_get_posts_for_menu( $menu_name ) {
// Read all of the navigation menu locations
$locations = get_nav_menu_locations();
// Grab the specific menu identified by the specified menu name.
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
// Grab all of the post IDs that are represented in this menu
$post_ids = array();
foreach( $menu_items as $post ) {
$post_ids[] = $post->object_id;
}
return $post_ids;
}
<?php
$args = array(
'post_type' => array( 'post', 'page', 'video' ),
'post__in' => acme_get_posts_for_menu( 'my-custom-menu' ),
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'menu_order'
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post();
/* Display post content here */
}
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.