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(); ?>
pre_get_posts
and run your conditionals there. View The Codex