Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created May 13, 2014 12:18
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tommcfarlin/f42c548e780b7f472abc to your computer and use it in GitHub Desktop.
[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