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.