0

My page has three separate loops on it. The first retrieves the most commented post. The query then resets for the second loop and retrieves the 3 most recent posts, then resets again to get the three most recents posts after that. What I want is for the post that has the most comments to a) only be chosen from the last 24 hours and b) to be excluded from showing up in the 2nd and 3rd queries to prevent duplicate posts showing up.

How would I achieve this?

 <?php get_header(); ?>

            <div class="container_12 clearfix">

                <div class="grid_8 main-content">

                        <div class="grid_12 alpha omega">

                        <?php 

                        $args = array(
                        'posts_per_page' => 1,
                        'orderby' => 'comment_count'


                        );

                        $the_query = new WP_Query( $args );

                        ?>

                        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                            <a href="<?php the_permalink();?>"><h1 class="main-heading"><?php the_title(); ?></h1></a>
                            <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>

                            <?php the_excerpt(); ?>

                        <?php endwhile; else: ?>


                        <?php endif; ?>

                        <?php wp_reset_postdata(); ?>

                        </div>


                        <?php 

                        $args = array(
                        'posts_per_page' => 3,
                        'orderby' => 'most_recent'
                        );

                        $the_query = new WP_Query( $args );

                        ?>

                        <div class="grid_6 alpha">

                        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                            <a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
                            <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a>
                            <?php the_excerpt(); ?>

                        <?php endwhile; else: ?>

                        <?php endif; ?>



                        <?php wp_reset_postdata(); ?>



                        </div>

                        <?php 

                        $exclude = $most_recent;


                        $args = array(
                        'posts_per_page' => 3,
                        'orderby' => 'most_recent',
                        'offset'  => 3
                        );

                        $the_query = new WP_Query( $args );

                        ?>

                        <div class="grid_6 alpha">

                        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                            <a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
                            <p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a>
                            <?php the_excerpt(); ?>

                        <?php endwhile; else: ?>

                        <?php endif; ?>

                        <?php wp_reset_postdata(); ?>

                        </div>

                </div><!--maincontent-->

                        <?php get_sidebar(); ?>

            </div><!--endcontainer-->

    <?php get_footer(); ?>
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.