How to display posts by DAILY, WEEKLY & Others Various Type

Today i am going to show you the code to display posts by Daily, Weekly, Monthly, Yearly & some other few custom ways those are really become necessary somewhere, somehow indeed.

First we will look into the ‘Daily’ based posts where the below methods can help you. For your learning purpose, i have tried to give all the possible ways that i know:

POST FOR TODAY

WAY-1:

$current_day = date('j');
query_posts('day='.$current_day);
if (have_posts()) :
     while (have_posts()) : the_post(); ?>
         // WordPress loop
     endwhile;
endif;

 

WAY-2:

$today = getdate();
$args = array(
	'date_query' => array(
		array(
			'year'  => $today['year'],
			'month' => $today['mon'],
			'day'   => $today['mday'],
		),
	),
);

$wp_query = new WP_Query( $args );
if (have_posts()) :
     while (have_posts()) : the_post(); ?>
         // WordPress loop
     endwhile;
endif; 

 

POST FOR RECENT WEEK/LAST WEEK

WAY-1:

$args = array(
	'date_query' => array(
		array(
			'year' => date( 'Y' ),
			'week' => date( 'W' ),
		),
	),
);
$wp_query = new WP_Query( $args );
while (have_posts() ) : the_post(); 
  //your stuff
endwhile; 

WAY-2: (available after WordPress 3.7)

$args = array(
 'post_type' => 'post',
 'post_status' => 'publish',
 'orderby' => 'date',
 'order' => 'DESC',
 'date_query' => array(
  array(
   'after' => '1 week ago'
  )
 )
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
 while ( $my_query->have_posts() ) {
 //your stuff
}
}
wp_reset_postdata();

 

 

For monthly display posts there have archive format but if you want to display the last 30 days posts then you have to use a handy code here:

POST FROM LAST 30 DAYS:

WAY-1:

 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');
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      );
$my_query=new WP_Query($args);
remove_filter('posts_where', 'filter_where');

  if( $my_query->have_posts() ) {
   
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
     //your stuff
    endwhile;
  } 
wp_reset_query();

 

WAY-2:

$args = array(
	'date_query' => array(
		array(
			'column' => 'post_date_gmt',
			'after'  => '1 month ago',
		)
	),
	'posts_per_page' => -1,
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    //your stuff
    endwhile;
  } 
wp_reset_query();

 

============================ VARIOUS DATE QUERY POSTS ==========================

 

Returns posts dated December 16, 2013:

$args = array(
	'date_query' => array(
		array(
			'year'  => 2013,
			'month' => 12,
			'day'   => 16,
		),
	),
);
$query = new WP_Query( $args );

 

Return posts between 9AM to 5PM on weekdays:

$args = array(
	'date_query' => array(
		array(
			'hour'      => 9,
			'compare'   => '>=',
		),
		array(
			'hour'      => 17,
			'compare'   => '<=',
		),
		array(
			'dayofweek' => array( 2, 6 ),
			'compare'   => 'BETWEEN',
		),
	),
	'posts_per_page' => -1,
);
$query = new WP_Query( $args );

 

Return posts from January 01, 2014 TO February 28, 2014:

$args = array(
	'date_query' => array(
		array(
			'after'     => 'January 1st, 2014',
			'before'    => array(
				'year'  => 2014,
				'month' => 2,
				'day'   => 28,
			),
			'inclusive' => true,
		),
	),
	'posts_per_page' => -1,
);
$query = new WP_Query( $args );

 

Return posts made over a year ago but modified in the past month:

$args = array(
	'date_query' => array(
		array(
			'column' => 'post_date_gmt',
			'before' => '1 year ago',
		),
		array(
			'column' => 'post_modified_gmt',
			'after'  => '1 month ago',
		),
	),
	'posts_per_page' => -1,
);
$query = new WP_Query( $args );

 

That’s all for today. Still if you face any problem feel free to drop a message here. Thanks.

This post has already been read 4186 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.