WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

looking for some code review.

I had an issue where I had to list events by their year. This was sorted by a meta_key ( "show_date" ).

It works as intended, though I'm interested to see if there is another way to do this that is more efficient.

I've commented out my code to shed light on my though process.

<?php get_header(); ?>

$args = array(
    'post_type' => 'event',
    'category_name' => 'past',
    'meta_key' => 'show_date',
    'order_by' => 'meta_value'

$prod_history = new WP_Query( $args );

//set up the arrays to store the years so I can iterate over them easily later
$all_years = array();

if ( $prod_history->have_posts() ) :

<?php while( $prod_history->have_posts() ): $prod_history->the_post(); ?>
        $date_str = get_field( 'show_date' );
        //seperate the date from format 'MONTH day(int), YYYY, giving us only year       
        $show_year = explode(',', $date_str);

        //reassign $show_year to an interger instead of a string
        $year_num = intval( $show_year[1] );        
        $link = get_permalink();
        $title = get_the_title();  

        array_push( $all_years, $year_num );

        $dates[] = array(
            $year_num => array (
                'title' => $title,
                'link' => $link

<?php endwhile; endif; ?>

    $start_year = min( $all_years );
    $end_year = max( $all_years );

    foreach( range( $end_year, $start_year ) as $year ){
        echo "<h2>" . $year . "</h2>";
        foreach( $dates as $date ){
            foreach ( $date as $key => $value ) {
                if( $key == $year ) {
                    echo '<h3><a href="' . $value["link"] . '" class="link" >' . $value['title'] . "</a></h3>";

Was setting up my multidimensional array '$dates' the best way of doing so?

Was my four deep foreach statement the most efficient way?

Did I overlook a much simpler solution?

Just trying to be a better programmer, thanks for the help.

share|improve this question

if you fix your date format to be yyyy-mm-dd they'll just naturally order themselves and you won't have to do all this complicated output.

share|improve this answer
+1 for simplicity | 2nd alternative would be timestamps... – Johannes Pille Jun 13 '12 at 15:16
Indeed. But then how would I handle placing a large header with the year for those specific events? – Squadrons Jun 13 '12 at 15:24
@Squadrons - extract year from first post, assign to $current_year, output header, then post. for each subsequent post, check if year matches $current_year, if not - output new year header and set $current_year to that year. – Milo Jun 13 '12 at 15:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.