• 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)
  • thrillphase

    (@thrillphase)

    I wanted to thank you for pointing me in the right direction and provide the code that is currently working for me. (wp 3.7.1)
    -in functions.php:

    // Menu item IDs
    function grab_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);
    		$theidlist = array();
    			foreach ( (array) $menu_items as $key => $menu_item ) {
    				$theidlist[] = $menu_item->object_id;
    			}
    		return $theidlist;
    		} else { return; }
    }

    -and the query:

    $menuitems = grab_menu_items();
        /* Query */
    	$the_query = new WP_Query( array( 'post_type' => 'page', 'orderby' => 'post__in', 'posts_per_page' => '-1', 'post__in' => $menuitems ) );
        /* The Loop */
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
       		<div id="box<?php the_ID(); ?>" class="shadow-box"><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">read more</a></div>
        <?php endwhile; /* Restore original Post Data */ wp_reset_query();

Viewing 1 replies (of 1 total)
  • The topic ‘Include specific pages in query and sort by order in menu’ is closed to new replies.