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.

probably tough to describe:

I have a custom-post-type called wr_event and a custom-field for it called event_date.

I have a lot of posts and each of those posts has set an event_date. Pretty straight forward.

All I'd like to do now is list all the years in which events have been posted. So I'd like to have this …

2012 | 2011 | 2010 | 2009 | 2008

How do I do that? I thought of looping through all posts and listing the years, but somehow I can't figure out how to do so.

  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
      global $post;
      $this_year = get_post_meta( $post->ID, 'event_date', true );
      $this_year = date('Y', $this_year);
      echo $this_year;
  endwhile;

What's the best and simplest solution to do so?

UPDATE

    rsort( $years ); // sorts the years array into reverse order
    foreach ($years as $year) {
        echo '<a href="#'.$year.'" name="y'.$year.'" class="inactive">'.$year.'</a> <span class="sep">|</span> ';
    }
share|improve this question
1  
How are your dates stored? What is the format? –  s_ha_dum Jan 20 '13 at 23:06
    
If they are timestamp-based then you can use a numeric ordering within the query instead of retroactively ordering (faster). –  Dan Jan 22 '13 at 14:17
add comment

1 Answer

up vote 1 down vote accepted

something like this should do the job:

global $post;
$years = array();
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    $this_year = get_post_meta( $post->ID, 'event_date', true );
    if ( $this_year = date( 'Y', $this_year ) ) {
        if ( ! in_array( $this_year, $years ) ) {
            $years[] = $this_year;
        }
    }
endwhile;
rsort( $years ); // sorts the years array into reverse order
echo implode( ' | ', $years );

to reduce queries, you can save everything into a transient:

global $post;
$transient = 'all-post-years';
$timeout   = 14400; // 4 hours

if ( false === $out = get_transient( $transient ) ) {

    $years = array();
    $loop  = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
        $this_year = get_post_meta( $post->ID, 'event_date', true );
        if ( $this_year = date( 'Y', $this_year ) ) {
            if ( ! in_array( $this_year, $years ) ) {
                $years[] = $this_year;
            }
        }
    endwhile;

    rsort( $years ); // sorts the years array into reverse order

    foreach ( $years as $year ) {
        $out .= '<a href="#' . $year . '" name="y' . $year . '" class="inactive">' . $year . '</a> <span class="sep">|</span> ';
    }

    if ( $out ) {
        set_transient( $transient, $out, $timeout );
    }

}

echo $out;
share|improve this answer
    
Alright, thank you. My $args array looks like this … $args = array( 'post_type' => 'wr_event', 'posts_per_page' => -1, // show all posts 'meta_key' => 'event_date', ); Is that a problem performansewise? I'm looping through all posts a second time on this page? –  mathiregister Jan 21 '13 at 8:03
    
i've updated the answer to show how you can cache the results using the transients API, so the query is not run on every pageload. –  Simon Blackbourn Jan 21 '13 at 23:02
    
Thank you, however that doesn't work for me. echo $out is always empty. Moreover I updated my question with the foreach loop I do on $years. I do not want to implode the array. Any last ideas why the code you posted doesn't work for me? I tried it exaclty like you suggested but echo $out doesn't show anything. –  mathiregister Jan 22 '13 at 8:03
    
i've updated my answer based on your foreach loop. hard to say why it isn't working - try putting in print_r($years) to check if the array has something in it. you'll also need to delete the transient as it lasts for 4hrs. set it to 10 seconds while you're debugging. –  Simon Blackbourn Jan 22 '13 at 14:19
    
Thank you. Perfect. –  mathiregister Jan 22 '13 at 15:32
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.