Question

I am working on a site with three different main 'topevel' sections, that act as the top level parents of the site.

Each toplevel section has children and grandchildren, and there needs to be a listing of the top 4 most popular descendant pages of each child section. I am currently gathering the most popular pages using a metakey, which works great. the wp_query I'm using however is only returning immediate children.

get_pages won't work here because the metakey sorting is not working, nor will it allow me to limit to just 4 pages . This is my current code.

<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'post__not_in'=> array(220), 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_type' => 'page', 'post_parent' => $parentid  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
                   <a href="<?php the_permalink(); ?>" class="cta"><?php the_title(); ?></a>
<?php endwhile;
                       wp_reset_postdata(); ?>
Était-ce utile?

La solution

Ok, I found a solution. I first looped through and got all the child page ID's, created an array, and used post_parent__in

<?php $parentarray = array($parentid);
                            $parentposts = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'page', 'post_parent' => $parentid ) );
                            while ( $parentposts->have_posts() ): $parentposts->the_post(); ?>
                                <?php $parentarray[] = get_the_id(); ?>
                        <?php endwhile; 
                       wp_reset_postdata(); ?>

<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'post__not_in'=> array(220), 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_type' => 'page', 'post_parent__in' => $parentarray  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
                   <a href="<?php the_permalink(); ?>" class="cta"><?php the_title(); ?></a>
<?php endwhile;
                       wp_reset_postdata(); ?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top