Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Top 30 Songs using Custom Post Type

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

Related Posts:

  1. WordPress Orderby Numeric Value Not Working
  2. Show the same Article Available in Other Categories
  3. Post image in WordPress not appearing on home page
  4. Update Post Meta for a logged in user
  5. Two Custom Post Types Many to Many Relationship
  6. Update postmeta Parent when post_status child change
  7. Batch Extract Date from post title and put into ACF custom field
  8. Get_post_custom not fetching value from array wordpress
  9. Add a form in every post and save data in post meta
  10. SELECT custom post type and its meta in SQL
  11. Save data is post using php front end
  12. Show posts from WP Custom Post Type selected from a field in a metabox
  13. How to properly get the wp_postmeta.meta_value of a custom post type in specifics?
  14. update a posts of other custom post type
  15. Edit post meta direct from post.php?
  16. How to create content automatically when a post is published?
  17. Display a post count from a custom metabox selection
  18. Automatically add custom taxonomy when meta value gets to a set number or beyond
  19. I am having a problem with fetching product data in the Gutenberg block editor
  20. Display random posts, but omit the post it is on?
  21. How do I do this with WordPress? Taxonomies?
  22. Disable block with taxonomies at post page
  23. How to implement a Google map store locator
  24. I would like to have different styles for my posts based on the content of each post
  25. How can i add thumbnails images to particular post (using code not admin pannel) in wordpress
  26. Showing posts from different categories and from custom post type
  27. Get latest 3 posts from multiple CPT in one query
  28. Deleting Custom Posts & Meta Data on Uninstall
  29. Widget: Custom Post Type Post Listing Dropdown on Admin Side
  30. Allow non-logged in users to see a future post after clicking on a list of future posts
  31. Which post does a taxonomy term belongs to?
  32. List custom taxonomy specific to one custom post type
  33. How to add a custom-post-type post within another custom-post-type post edit screen using AJAX?
  34. Meta query for custom post type ignored in main query
  35. Changing custom type name hides the posts
  36. How do I create an archive page as a including metadata?
  37. Display Ad on Specific Categories
  38. Hiding posts by other users and non-logged in
  39. changing meta value and meta key of price field
  40. Posting to a Custom Post Type from front end – user generated content
  41. Getting meta in editor plugin, and event triggering issue
  42. Add custom field to Posts and sort by it
  43. How to duplicate entire custom post type
  44. Add custom post type settings to wordress default posts
  45. A better way to add a meta box to custom post types
  46. Restrict Access to Posts based on Custom User and Post Meta Data
  47. Get related posts of child term of custom post type
  48. Problem with displaying posts in the CPT category
  49. get_post_meta not working on publishing
  50. Should wp_postmeta meta_key always be unique for a given post_id?
  51. JS innerhtml changing style when using AJAX
  52. Show titles, date of all posts on single category page
  53. how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?
  54. get_post_meta returning no value in custom post type alert email
  55. Side effects of Script and Iframe in post
  56. post meta parameter in post custom-post-type endpoint with restapi
  57. How do I set all of a particular post meta to a value within the custom post type I’m in?
  58. How to change the post type a theme shows by default?
  59. Set a checkmark in a category based on a URL-parameter
  60. Several post types on WP Query by tag and taxonomy
  61. No Permission to add new Page, Post or CPT with Admin role
  62. Metabox not show in categories custom post type cmb2
  63. custom post type category count shortcode
  64. WordPress request fiter order by related post’s post_title
  65. Portfolio Page for Classic Posts
  66. Add post location with mile radius allowing search
  67. Get the category from custom post type
  68. Is it possible to store Custom Post Type data in separate set of tables and still have wp_post class functionality?
  69. Set up post page like JAMA articles
  70. Custom Post Slug same as Parents Category Slug
  71. Shortcode not working with post counter
  72. Changing default ‘posts’ parameters with register_post_type_args
  73. Admin Column does not populate with data
  74. Update database from Quick Edit according to Checkbox state of Custom Post Type
  75. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  76. How to integrate a form (Ninja Form or Contact Form 7) with Custom Post Types?
  77. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  78. Dynamic Custom Fields
  79. Prioritize posts in query by meta keys?
  80. Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
  81. Show Custom Post Type meta boxes only on Page Edit
  82. Allow users to create posts without logging in?
  83. Custom filter function not working with Custom post type
  84. How to get all tags of a custom post type by id
  85. How to define which register_post_status goes to which register_post_type?
  86. Load Next Posts With AJAX not working with custom post type
  87. Limit the post for differents custom post type in the same wp_query
  88. 2 Different Custom Post Types in Submenu
  89. How to assign classes to all elements?
  90. Importing Data from a Non-WordPress database, into WP
  91. How can I show 1 featured post in a styled element, and the next few below differently styled
  92. Getting thumbnails of a custom posts featured images in a page-templates metabox
  93. Some posts from custom post type to subdomains
  94. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  95. Remove Post Custom Meta Box
  96. Can’t publish custom post type – “You are not allowed to edit this post.”
  97. How can i create an custom post template for an specific post category? [closed]
  98. Custom fields for custom post type
  99. Show future events based on custom field AND order by date
  100. How to sort posts in a custom post type by title in ascending order by default?
Categories custom-post-types Tags custom-post-types, post-meta, posts
Path for contact form file
reorder a WP_Query, using a dropdown

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress