Include specific pages in query and sort by order in menu
-
I’m working on a single scroll site. I want to include ONLY pages which appear in the primary menu of the site. If a page is not in the primary menu, it should NOT be included in the loop. I also want the order of pages output by the query to be in the same order which they are in the menu. Here is what I have so far:
$page_sort_sections = sort_sections(); $args = array( 'posts_per_page' => -1, 'post_type' => 'page', 'post__in' => (array) $page_sort_sections, 'orderby' => 'post__in', ); $home_query = new WP_Query($args); if($home_query->have_posts()): while($home_query->have_posts()) : $home_query->the_post(); get_template_part('page-sections'); endwhile; else: get_template_part( '404'); endif;
That takes care of setting up the query.
Here is the code of the function which I am using to pass in the post__in array:
if ( ! function_exists( 'sort_sections' ) ){ function sort_sections(){ if(!has_nav_menu( 'primary' )){ return; } if ( ( $locations = get_nav_menu_locations() ) && isset( $locations['primary'] ) ) { $menu = wp_get_nav_menu_object( $locations['primary'] ); $items = wp_get_nav_menu_items($menu->term_id); $sections = array(); foreach((array) $items as $key => $menu_items){ if('page-sections' == $menu_items->object){ $sections[] = $menu_items->object_id; } } return $sections; } } }
I did a var_dump on the query and its returning:
["post__in"]=> array(0) { } ["orderby"]=> string(8) "post__in" }
Any thoughts on why this not returning the IDs of the items in the menu?
Thanks ??
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Include specific pages in query and sort by order in menu’ is closed to new replies.