Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.