• Resolved stephensaid

    (@stephensaid)


    Hi all,

    I am trying to display all the children of a page but I want to exclude any pages that have children of their own.

    In the below code, the WP_Query is actually holding all the pages, including those with children. I am only not outputting them which is creating havoc in pagination.

    I am new to WordPress coding, so anything that you see which is not right, please give me a shout and let me know.

    		        global $post;
    			$ourCurrentPage = get_query_var('paged');
    			
    			$mypages = new WP_Query(array(
    				'post_type' => 'page',
    				'post_parent' => $post->ID,
    				'showposts' => 10,
    				'paged' => $ourCurrentPage,
    				'order' => desc,
    				'orderby' => ID
    			));
    			
    			if ($mypages->have_posts()) : 
    				while ($mypages->have_posts()) : 
    					$mypages->the_post();
    					$children = get_pages( array( 'child_of' => $post->ID ) );
    					$count_of_children = count( $children );
    					
    					if( $count_of_children == 0 ) {
    						?>
    						<div class="bdl-child-pages">
    							<h2><?php the_title(); ?></h2> 		
    							<div class="entry page-<?php echo the_ID(); ?>"><?php echo the_content(); ?></div> 
    							<div class="bdl-read-more"><a class="button" href="<?php the_permalink(); ?>">Read more...</a></div>
    						</div>
    						<?php
    						//return false;
    					} else {
    						$pages_with_children++;
    					}
    				endwhile;
    				
    				?>
    				<div class="bdl-pagination woocommerce-pagination">
    					<?php
    					//echo paginate_links(array('total' => $mypages->max_num_pages - $pages_with_children));
    					//echo '<br>';
    					echo paginate_links(array('total' => $mypages->max_num_pages));
    					?>
    				<div>
    				<?php
    			
    			endif;
    			
    			wp_reset_postdata();
    			?>		
    

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think what you want is possible with a simple WP_Query. You’d probably need to construct your own SQL query and execute it using $wpdb methods.

    Or maybe another possibility could be to make multiple queries. Start with your query as is, and identify child posts with children the same way, but do not generate any output yet. Assemble all immediate children post IDs who have children of their own, which you want excluded. Make a new query for immediate children, but this time include the array of unwanted IDs as the “post__not_in” argument. The results this time will be as desired and the pagination should work correctly.

    A custom SQL query would be more efficient, but I’ve no idea myself on how that might be constructed.

    Thread Starter stephensaid

    (@stephensaid)

    Hi bcworkz,

    Thanks for your suggestions. I am not sure how to do it using sql so went with your other suggestion and this is what I’ve got. – and it works.

    
    			<?php
    			
    			global $post;
    			$ourCurrentPage = get_query_var('paged');
    			
    			$thispost = $post->ID;
    			
    			$allmypages = new WP_Query(array(
    				'post_type' => 'page',
    				'post_parent' => $post->ID,
    				'showposts' => -1
    			));			
    			
    			$i = 0;
    			if ($allmypages->have_posts()) :
    				while ($allmypages->have_posts()) :
    					$allmypages->the_post();
    					$children = get_pages( array( 'child_of' => $post->ID ) );
    					$count_of_children = count( $children );
    					/* echo $post->ID; ?> - <?php echo the_title(); ?> - <?php echo $count_of_children . '<br>'; */
    					if( $count_of_children > 0 ) {
    						$pageswithchildren[++$i] = $post->ID;
    					}
    				endwhile;
    			endif;
    
    			$mypages = new WP_Query(array(
    				'post_type' => 'page',
    				'post_parent' => $thispost,
    				'showposts' => 10,
    				'post__not_in' => $pageswithchildren,
    				'paged' => $ourCurrentPage,
    				'order' => desc,
    				'orderby' => ID
    			));
    
    			?>
    			<div class="bdl-pagination woocommerce-pagination">
    				<?php
    				echo paginate_links(array('total' => $mypages->max_num_pages));
    				?>
    			</div>
    			<?php
    			
    			if ($mypages->have_posts()) : 
    				while ($mypages->have_posts()) : 
    					$mypages->the_post();
    						?>
    						<div class="bdl-child-pages">
    							<h2><?php the_title(); ?></h2> 		
    							<div class="entry page-<?php echo the_ID(); ?>"><?php echo the_content(); ?></div> 
    							<div class="bdl-read-more"><a class="button" href="<?php the_permalink(); ?>">Read more...</a></div>
    						</div>
    						<?php	
    				endwhile;
    				
    				?>
    				<div class="bdl-pagination woocommerce-pagination">
    					<?php
    					echo paginate_links(array('total' => $mypages->max_num_pages));
    					?>
    				</div>
    				<?php
    			endif;
    			
    			wp_reset_postdata();
    			?>		
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query including all child pages excluding pages with children of their own’ is closed to new replies.