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

I have successfully created a Top 30 songs ranking via custom post type. Now im stuck with adding "Peak Position" post meta. I don't know how to program it. Peak Position is the position where the song ranked for a couple of weeks.

For example, Song 1 has been ranked 5 for 10 weeks and then down to 6 position for 3 weeks.. so the Peak Position for Song 1 is 5 since it lasted for 10 weeks.

Now i'm stucked thinking how can i save its peak position using post meta.

Can anyone suggest?

share|improve this question
How is that Peak Position defined? Does it only relate to how many weeks the song has been in that position. Or only the highest position ever, no matter how many weeks. – micadelli Mar 1 '13 at 10:39
Do you have a currently working code? Could you post what you already have? – brasofilo Mar 1 '13 at 10:42

If I understood correctly, that Peak Postion is based on longest period the song was in same position (which I found odd), here's what you could do:

  • Have three meta fields
    :: song_meta_peak
    :: song_meta_weeks_now
    :: song_meta_weeks_peak

  • First time song enters the list, you set peak meta to whatever the position is and both weeks_now and weeks_peak metas to 1

  • Every week, if position is not changed, you add 1 to weeks_now meta

  • Whenever the position changes you first check the weeks_now meta to find out how long the song has been in the previous position, and if it exceeds the weeks_peak you set peak meta to that previous position and override weeks_peak to match the weeks_now. After that you set weeks_now back to 1.

Hope that helps

Code Example

$oldWeeks = get_post_meta( $post_id, 'song_meta_weeks_now', true );
$oldWeeksPeak = get_post_meta( $post_id, 'song_meta_weeks_peak', true );

    update_post_meta( $post_id, 'song_meta_weeks_now', $oldWeeks + 1 );
} else {
    update_post_meta( $post_id, 'song_meta_weeks_now', 1 );
    if ( $oldWeeks > $oldWeeksPeak ) {
        update_post_meta( $post_id, 'song_meta_weeks_peak', $oldWeeks );
        update_post_meta( $post_id, 'song_meta_peak', PREVIOUS POS. );
share|improve this answer

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.