0

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?

2
  • 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
    Commented Mar 1, 2013 at 10:39
  • Do you have a currently working code? Could you post what you already have?
    – brasofilo
    Commented Mar 1, 2013 at 10:42

1 Answer 1

0

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

if ( POS. NOT CHANGED ) {
    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. );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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