php - How correct code in WordPress sidebar to show only posts from last 1 month from category -
i started work on 1 website after developer , don't understand how make popular widget show posts last 3 months categories(1 month category news). see there switch statement should it, not doing job. how 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 trying replace inside switch
case 'news': $args['interval'] = '1 month'; break;
with
case 'news': // create new filtering function add our clause query function filter_where( $where = '' ) { // posts in last 30 days $where .= " , 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 , remove_filter( 'posts_where', 'filter_where' ); break;
and
case 'analysis': $args['interval'] = '3 month'; break;
with
case 'analysis': // create new filtering function add our clause query function filter_where( $where = '' ) { // posts in last 30 days $where .= " , 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 $args
query particular period. see time parameter documentation.
example:
// returns posts current week $week = date('w'); $year = date('y'); $query = new wp_query( 'year=' . $year . '&w=' . $week );
Comments
Post a Comment