0

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;?>&amp;w=100&amp;h=100&amp;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(); ?>
3
  • How is where filter used so that is causes the issue? It essentially is the way to go for your requirement.
    – Rarst
    Jun 15, 2013 at 22:18
  • I added the other query to the question above.
    – Chozen
    Jun 16, 2013 at 3:53
  • By chance by "not being able to use filter_where()" do you mean that you cannot define function with that name twice? Note that it doesn't have to be named filter_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.
    – Rarst
    Jun 16, 2013 at 14:39

1 Answer 1

1

For creating a date filter you can use

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 7 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

You can also see here Time Parameter

1
  • Did you see my note?
    – Chozen
    Jun 15, 2013 at 14:43

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.