我正在使用WordPress插件'我推荐这个',它允许人们喜欢'帖子。 该值存储为元键,我们可以查询该元键以生成“最推荐的”元素。页。
插件 - > http://wordpress.org/support/plugin/i-recommend-this
循环。
<?php
query_posts('&orderby=meta_value&meta_key=_recommended');
if (have_posts()): while (have_posts()) : the_post();
?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<?php endif; ?>
这有效,它找到了最推荐的帖子。
两个问题。
如何将返回的帖子限制为最近3个月的日期范围?
以类似的方式,我怎么能有一个按钮,本周最受欢迎的按钮&#39;是否允许用户查看过去7天内过滤的帖子结果?
答案 0 :(得分:0)
你可以尝试这样的事情,这只是一个基本的例子:
$today = getdate();
$threemonths = $today["mon"]-3;
$args = array(
'date_query' => array(
array(
'after' => array(
'year' => $today["year"],
'month' => $threemonths,
'day' => $today["mday"],
),
'before' => array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
他们在wordpress文档中进一步细分:
http://codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
答案 1 :(得分:0)
我解决了这个问题。使用自定义过滤器和WP_query数组。
<?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('-180 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$featuredPosts = new WP_Query( array(
'meta_key'=>'_recommended',
'orderby' => 'meta_value_num',
'order' => DESC
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<div class="figure">
<?php the_post_thumbnail('frontthumb'); ?>
</div>
<div class="fig-cover">
<div class="fig-bottom">
<div class="title-container">
<?php if ( get_post_meta($post->ID, 'Price', true) ) { ?>
<div class="price-container">
<?php echo get_post_meta($post->ID, "Price", true); ?>
</div>
<h2 class="price-title"><?php the_title(); ?> </h2>
<?php } else { ?>
<h2><?php the_title(); ?> </h2>
<?php } ?>
</div> <!-- end div title-container -->
</div>
</div>
<div class="reco">
<?php if( function_exists('dot_irecommendthis') ) dot_irecommendthis(); ?>
</div>
<a class="the-post-link" href="<?php the_permalink(); ?> ">
</a>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
现在我只需要弄清楚如何在现场进行实时过滤......