我有以下查询,它与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;?>&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(); ?>