将日期过滤器添加到热门帖子查询

时间:2013-06-15 作者:Chozen

我有以下查询,它与functions.php 文件:

<?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(); ?>
我想添加一个日期过滤器,只显示过去7天的帖子。

我该怎么做?

Note: 它不能使用filter_where(), 它正在页面的其他位置使用,如果在同一页面上使用多次,则会导致错误。

下面是我在导致冲突的页面上已有的查询。

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

1 个回复
SO网友:ksr89

用于创建日期筛选器,可以使用

// 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\' );
你也可以在这里看到Time Parameter

结束

相关推荐

Restrict query_posts by Date?

要根据页面视图显示最流行的帖子,我使用以下代码(source):<?php query_posts(\'meta_key=post_views_count&orderby=meta_value_num&order=DESC\'); if (have_posts()) : while (have_posts()) : the_post(); ?> <li><a href=\"<?php the_permali