I have the following query, which was put together with snippets in the functions.php
file:
<?php
$popularpost = new WP_Query( array(
'posts_per_page' => 5,
'meta_key' => 'wpb_post_views_count',
'post_type' => array( 'post', 'music', 'videos', 'albums' ),
'tax_query' => array(
array(
'taxonomy' => 'content',
'field' => 'slug',
'terms' => array( 'indy' ),
'operator' => 'NOT IN'
)
)
));
while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
I would like to add a date filter to only display posts from the last 7 days.
How would I do that?
Note: It can't use filter_where()
, it's being used elsewhere on the page and results in a error if used more than once on the same page.
Below is the query I already have on the page that's causing the conflict.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query( array(
'posts_per_page' => '5',
'v_sortby' => 'views',
'post_type' => array( 'post', 'music', 'videos', 'albums' ),
'tax_query' => array(
array(
'taxonomy' => 'content',
'field' => 'slug',
'terms' => array( 'indy' ),
'operator' => 'NOT IN'
)
)
));
remove_filter( 'posts_where', 'filter_where' );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<div id="widget-post-wrapper">
<a class="widget-post-title" href="<?php the_permalink() ?>" rel="bookmark">
<?php
// this is where title of the Feature gets printed
the_title(); ?></a>
<a class="thumbnails-link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<img class="widget-thumbnails" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,'full'); $image_url = $image_url[0]; ?><?php echo $image_url;?>&w=100&h=100&zc=1" />
</a>
<?php the_time('F j, Y'); ?>
<br />
<?php if(function_exists('the_views')) { the_views(); } ?>
<br />
<?php if ('music' == get_post_type() ) { ?>
<a href="">Music</a>
<?php } ?>
<?php if ('post' == get_post_type() ) { ?>
<a href="">Blog</a>
<?php } ?>
<?php if ('videos' == get_post_type() ) { ?>
<a href="">Videos</a>
<?php } ?>
<?php if ('albums' == get_post_type() ) { ?>
<a href="">Albums</a>
<?php } ?>
</div>
<?php endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata(); ?>
filter_where()
" do you mean that you cannot define function with that name twice? Note that it doesn't have to be namedfilter_where()
, it can be arbitrary function(s) to be used with filter. It's also good idea to use more specific names for them, with some kind of custom prefix for example.