I have a custom field value called points
.
- Post 01 points = 5
- Post 02 points = 10
- Post 03 points = 8
I need to create another custom filed value called rank
.
Rank custom filed value should be like this.
- Post 01 rank : 1
- Post 02 rank : 3
- Post 03 rank : 2
It is totally based on other custom filed value. It should be change every time when point custom filed value change.
Is it possible or there is an alternative way to do it?
PS : Basically what I need to do it when a post view I need to display according to points this is a 5th post…. like wise…
1 Answer
I recommend you look into the get_{$meta_type}_metadata filter. This is a basic hook setup to return a dynamic post meta value:
<?php
add_filter( "get_post_metadata", function( $points, $post_id, $meta_key, $single ){
if ( 'points' == $meta_key && $single ) {
update_post_meta( $post_id, 'rank', calculate_post_rank( $post_id, $points ) );
}
return $points;
}, 10, 4 );