I have a custom post type called " events ".
I wanted to make a list of months from a year that is clickable and then when click it will display all the post content from that month.
So whenever I click on a month for example I clicked on "january" of 2017.
It will show all posts from that month under it. example below
I have here a code which lists the months but links are not working
<?php
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'events' GROUP BY month , year ORDER BY post_date ASC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
if ($year_prev != null){?>
<?php } ?>
<div class="archive-year"><strong><?php echo $month->year; ?></strong></div>
<?php } ?>
<div><a href="#"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></div>
<?php $year_prev = $year_current;
if(++$limit >= 18) { break; }
endforeach; ?>
sorry but I do not know how this works, thanks in advance
I have made this function. in my function.php file
function post_type_Custom($where,$args){
$post_type = isset($args['post_type']) ? $args['post_type'] : 'post';
$where = "WHERE post_type = '$post_type' AND post_status = 'publish'";
return $where;
}
add_filter( 'getarchives_where','post_type_Custom',10,2);
now add following code wherever you want to display list by monthly
$args = array(
'post_type' => 'custom_post_type',
'type' => 'monthly',
'echo' => 0
);
echo '<ul>'.wp_get_archives($args).'</ul>';
Try this
You Can do it using WordPress date_query
$args = array(
'post_type' =>'events',
'post_status' => 'publish',
'date_query' => array(
array(
'year' => 2017,
'month' => 1
),
),
);
$query = new WP_Query( $args );
$events = $query->posts;
foreach ($events as $event) {
$event->ID:$event->post_title.<br>;
}
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.