0

What I'm trying to do is this:

If in the last 7 days someone has added a post in the category 'featured' display it in the loop. Otherwise, display the most commented post from the last seven days.

So far I can display the most commented post but I'm not sure how to go about setting up the if/else block or if there is built in functionality in the wordpress core.

<?php 


   // retrieve posts from the last seven days

    function filter_where( $where = '' ) {
        // posts in the last 7 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
        return $where;
    }



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

                    add_filter( 'posts_where', 'filter_where' );

                    $the_query = new WP_Query( $args );

                    remove_filter( 'posts_where', 'filter_where' );



                    ?>

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

                    <?php $do_not_duplicate = $post->ID; ?>

                        <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(); ?>

                        <hr>

                    <?php endwhile; else: ?>


                    <?php endif; ?>

                    <?php wp_reset_postdata(); ?>
1
  • You can probably use pre_get_posts and run your conditionals there. View The Codex
    – Howdy_McGee
    Mar 27, 2014 at 0:52

1 Answer 1

0

You can simplify things by using the date_query of WP_Query(), instead of the posts_where filter.

You can then try the following (untested):

// Fetch from the 'featured' category
$args = array(                
    'posts_per_page' => 1,                    
    'category_name'  => 'featured',
    'date_query'     => array( array( 'after' => '1 week ago' ) ),
);

$the_query = new WP_Query( $args );

if ( 0 === $the_query->found_posts )
{
    // Fetch the most commented post
    $args = array(                
        'posts_per_page' => 1,                    
        'orderby'        => 'comment_count',
    );
    $the_query = new WP_Query( $args );
}

// Your loop here:

if( $the_query->have_posts() ) : 
    while( $the_query->have_posts() ) : $the_query->the_post();
        // ...
    endwhile;
    wp_reset_postdata();
else:
    _e( 'No posts found!' );
endif;

Hope this help.

2
  • Thanks for the tip on date_query, makes it easier if I change the time and date in wordpress admin.
    – chap
    Mar 27, 2014 at 4:59
  • yes, the date query can be very handy - ps: I updated the answer to add a missing array for the date query.
    – birgire
    Mar 27, 2014 at 8:38

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.