Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Function to Group posts by month from Meta Key WordPress

  • SOLVED

I want to filter through posts categorized by months based on a custom field "date" rather than published date.

This code below filters through the posts and categorizes them by month based on post_date.




<?php

$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'event' ORDER BY post_date ASC");

foreach($years as $year) :

?>


<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'event' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");

foreach($months as $month) :

?>

<h2 class="whatsNew"><?php echo date( 'F', mktime(0, 0, 0, $month) );?> <?php echo $year; ?></h2>




<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'event' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");

foreach ($theids as $theid):

?>




<?php
$date = get_field('date', $theid->ID);
$month = date("M", strtotime($date));
$day = date("j", strtotime($date));
?>



<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">


<div class="col-sm-3">
<p class="date"><a href="<?php echo the_permalink(); ?>"><span class="month"><?php echo $month; ?></span><span class="day"><?php echo $day; ?></span></a></p>

</div>


<div class="col-sm-9">
<p class="title"><a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a></p>
<p class="location"><?php the_field('location', $theid->ID); ?></p>
<p class="time"><?php the_field('time', $theid->ID); ?></p>
</div>

</article> <!-- end article -->

<?php

endforeach; ?>



<?php endforeach;?>





<?php endforeach; ?>


<?php wp_reset_query(); ?>

Answers (2)

2014-04-15

Sébastien | French WordpressDesigner answers:

Try this code
Your custom field "date" must have this format : YYYY/MM/DD
Tell me if you use another format



<?php
$args = array(
'post_type' => 'event',
'meta_key' => 'date', //format YYYY/MM/DD
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
query_posts($args);

$year = "";
$month = "";
$day = "";
$year_used = "";
$month_used = "";

if (have_posts()) : while (have_posts()) : the_post();

$myDate = get_post_meta($post->ID, 'date', true);
$explode_date = explode("/",$myDate);
$year = $explode_date[0];
$month = $explode_date[1];
$day = $explode_date[2];

if($year_used !== $year) { ?>
<h2 class="whatsNew"><?php echo $year; ?></h2>
<?php
$year_used = $year;
if($month_used !== $month) echo "<h3>".$month."</h3>";
$month_used = $month; ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<div class="col-sm-3">
<p class="date"><a href="<?php echo the_permalink(); ?>"><span class="month"><?php echo $month; ?></span><span class="day"><?php echo $day; ?></span></a></p>

</div>


<div class="col-sm-9">
<p class="title"><a href="<?php bloginfo('url'); ?>?p=<?php echo $post->ID; ?>"><?php echo $post->post_title; ?></a></p>
<p class="location"><?php the_field('location', $post->ID); ?></p>
<p class="time"><?php the_field('time', $post->ID); ?></p>
</div>

</article> <!-- end article -->

<?php
endwhile; endif;
wp_reset_query();
?>


russell comments:

Date Field format is yymmdd.

This is giving a syntax error. unexpected endwhile.

Month Category heading should read just month year/
<h2 class="whatsNew"><?php echo $month; ?> <?php echo $year; ?></h2>


Sébastien | French WordpressDesigner comments:

The new code :)
try and let me know if that works

<?php
$args = array(
'post_type' => 'event',
'meta_key' => 'date', //format YY/MM/DD
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
query_posts($args);

$year = "";
$month = "";
$day = "";
$year_used = "";
$month_used = "";

if (have_posts()) : while (have_posts()) : the_post();

$myDate = get_post_meta($post->ID, 'date', true);

$year = substr($myDate, 0, 2);
$month = substr($myDate, 2, 2);
$day = substr($myDate, 4, 2);

if($year_used !== $year) { ?>
<!--<h2 class="whatsNew"><?php echo $year; ?></h2>-->
<?php }
$year_used = $year;
if($month_used !== $month) {?>
<h2 class="whatsNew"><?php echo $month; ?> <?php echo $year; ?></h2>
<?php }
$month_used = $month; ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<div class="col-sm-3">
<p class="date"><a href="<?php echo the_permalink(); ?>"><span class="month"><?php echo $month; ?></span><span class="day"><?php echo $day; ?></span></a></p>
</div>

<div class="col-sm-9">
<p class="title"><a href="<?php bloginfo('url'); ?>?p=<?php echo $post->ID; ?>"><?php echo $post->post_title; ?></a></p>
<p class="location"><?php the_field('location', $post->ID); ?></p>
<p class="time"><?php the_field('time', $post->ID); ?></p>
</div>

</article> <!-- end article -->

<?php
endwhile; endif;
wp_reset_query();
?>

2014-04-15

Arnav Joy answers:

why you don't try something like this

<?php
$args = array(
'post_type' => 'event',

'meta_key' => date', //name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
//Insert code here...
//Test to see if it is sorting the posts (below)
<?php $dateStart = get_post_meta($post->ID, 'date-start', true); echo $dateStart;?>

<?php endwhile; endif; wp_reset_query(); ?>