I started to work on one website after another developer and I don't understand how to make popular widget to show only posts from last 3 months from categories(1 month for category news). I see there is switch statement which should do it, but it is not doing its job. How to make intervals correctly?
function getSomePost($category,$postsPerPage=3) {
global $post;
$args = array(
'category_name'=>$category,
'posts_per_page'=>$postsPerPage,
'post_type'=>'post',
'post_status'=>'publish',
'post__not_in'=>array($post->ID),
'orderby'=>'rand',
);
switch ($category)
{
case 'news':
$args['interval'] = '1 MONTH';
break;
case 'analysis':
$args['interval'] = '3 MONTH';
break;
case 'reports':
$args['interval'] = '3 MONTH';
break;
}
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postThumbClass = 'no-thumb';
?>
<div <?php post_class(array('wp-post-entry', 'sidebar-post' )); ?>>
<?php if(has_post_thumbnail ()):?>
<?php $postThumbClass = '' ?>
<div class="wp-post-thumbnail">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(array(70,70)); ?>
</a>
</div>
<?php endif; ?>
<div class="wp-post-full-content <?php echo $postThumbClass ?> ">
<h3 class="wp-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<!--
<div class="post-content">
<?php the_excerpt() ?>
</div>
//-->
</div>
</div>
}
}
}
update
I am trying to replace inside switch
case 'news':
$args['interval'] = '1 MONTH';
break;
with
case 'news':
// 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' );
$query = new WP_Query( $query_string ); //I tried $args and everything
remove_filter( 'posts_where', 'filter_where' );
break;
and
case 'analysis':
$args['interval'] = '3 MONTH';
break;
with
case 'analysis':
// 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('-90 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
break;
You may use Time Parameter
in your $args
to query for a particular period. See Time Parameter documentation.
Example:
// Returns posts for just the current week
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.