Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>

<?php 
$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(); ?>
    <?php 
        $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; ?>

<?php
    $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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.