Skip to main content
added 64 characters in body
Source Link
kaiser
  • 51k
  • 27
  • 152
  • 246

Here are 2two simple wayways to do it, onethis:

One is to use the the_content filter hook-filter to update the post hotness"hotness", or you can schedule an event that will run daily and update the hotness once"hotness" once a day.

First approach: the_content filter

First approach: the_content-filter

function update_hotness( $post_id, $echo = false ) { $time_ago = human_time_diff( get_the_time( 'U', $post_id ),, current_time('timestamp') ); if ( strtotime($time_ago) < strtotime('1 day') ) { $time_ago = "1"; } $days_ago = preg_replace( "/[^0-9 ]/", '', $time_ago ); $days_ago; $ratings_score = get_post_meta( $post_id, 'ratings_score', true ); $hotness = $ratings_score / $days_ago; //here Here you store the hotness"hotness" as post meta update_post_meta( $post_id, 'hotness', $hotness ); if ( $echo ) echo $hotness; return $hotness; } 

soSo you can now get and update the post hotness"hotness" with this function by just passing it the post id.

nextNext you create a hooked function to 'the_contentfilter hook which will update the post hotness every timethe_contentthe_content`-filter which will update the posts "hotness" every time the_content is called:

add_filter( 'the_content','update_hotness_filter' ); function update_hotness_filter( $content ) { global $post; update_hotness( $post->ID ); return $content; } 

Second approach: scheduling an event

Second approach: Scheduling an event

First, create a function that will check if your event is scheduled or not the, then update accordingly:

add_action( 'wp', 'hotness_update_activation' ); function hotness_update_activation() { if ( ! wp_next_scheduled( 'daily_hotness_update' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'daily_hotness_update' ); } } 

nextFinally you make sure to add an action hook to your evenevent and a function that will run every day:

add_action( 'daily_hotness_update', 'daily_hotness_update_callback' ); function daily_hotness_update_callback() { // hereHere you get a list of posts to update the hotness"hotness" for and you just . // Then you just call update_hotness`update_hotness()` for each one ex: foreach ( $posts as $p ) { update_hotness( $p->ID ); } } 

andAnd that is it :).

If you ask me you should go with the simpler way, which is the first approach.

Here are 2 simple way to do it, one is use the_content filter hook to update the post hotness or you can schedule an event that will run daily and update the hotness once a day.

First approach: the_content filter

function update_hotness($post_id, $echo = false){ $time_ago = human_time_diff( get_the_time('U',$post_id),, current_time('timestamp') ); if (strtotime($time_ago) < strtotime('1 day')) { $time_ago = "1"; } $days_ago = preg_replace("/[^0-9 ]/", '', $time_ago); $days_ago; $ratings_score = get_post_meta($post_id,'ratings_score',true); $hotness = $ratings_score / $days_ago; //here you store the hotness as post meta update_post_meta($post_id, 'hotness', $hotness); if ($echo) echo $hotness; return $hotness; } 

so you can now get and update the post hotness with this function by just passing it the post id.

next you create a hooked function to 'the_contentfilter hook which will update the post hotness every timethe_content` is called:

add_filter('the_content','update_hotness_filter'); function update_hotness_filter($content){ global $post; update_hotness($post->ID); return $content; } 

Second approach: scheduling an event

First create a function that will check if your event is scheduled or not the update accordingly:

add_action('wp', 'hotness_update_activation'); function hotness_update_activation() { if ( !wp_next_scheduled( 'daily_hotness_update' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'daily_hotness_update'); } } 

next you make sure to add an action hook to your even and a function that will run every day:

add_action('daily_hotness_update', 'daily_hotness_update_callback'); function daily_hotness_update_callback() { // here you get a list of posts to update the hotness for and you just  // call update_hotness for each one ex: foreach ($posts as $p ) { update_hotness($p->ID); } } 

and that is it :).

If you ask me you should go with the simpler way which is the first approach.

Here are two simple ways to do this:

One is to use the the_content-filter to update the post "hotness", or you can schedule an event that will run daily and update the "hotness" once a day.

First approach: the_content-filter

function update_hotness( $post_id, $echo = false ) { $time_ago = human_time_diff( get_the_time( 'U', $post_id ), current_time('timestamp') ); if ( strtotime($time_ago) < strtotime('1 day') ) { $time_ago = "1"; } $days_ago = preg_replace( "/[^0-9 ]/", '', $time_ago ); $days_ago; $ratings_score = get_post_meta( $post_id, 'ratings_score', true ); $hotness = $ratings_score / $days_ago; // Here you store the "hotness" as post meta update_post_meta( $post_id, 'hotness', $hotness ); if ( $echo ) echo $hotness; return $hotness; } 

So you can now get and update the post "hotness" with this function by just passing it the post id.

Next you create a hooked function to the_content-filter which will update the posts "hotness" every time the_content is called:

add_filter( 'the_content','update_hotness_filter' ); function update_hotness_filter( $content ) { global $post; update_hotness( $post->ID ); return $content; } 

Second approach: Scheduling an event

First, create a function that will check if your event is scheduled or not, then update accordingly:

add_action( 'wp', 'hotness_update_activation' ); function hotness_update_activation() { if ( ! wp_next_scheduled( 'daily_hotness_update' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'daily_hotness_update' ); } } 

Finally you make sure to add an action hook to your event and a function that will run every day:

add_action( 'daily_hotness_update', 'daily_hotness_update_callback' ); function daily_hotness_update_callback() { // Here you get a list of posts to update the "hotness" for. // Then you just call `update_hotness()` for each one ex: foreach ( $posts as $p ) { update_hotness( $p->ID ); } } 

And that is it :)

If you ask me you should go with the simpler way, which is the first approach.

Bounty Awarded with 50 reputation awarded by CommunityBot
Source Link
Bainternet
  • 67.8k
  • 8
  • 132
  • 188

Here are 2 simple way to do it, one is use the_content filter hook to update the post hotness or you can schedule an event that will run daily and update the hotness once a day.

First approach: the_content filter

Turn you code in to a "callable" function:

function update_hotness($post_id, $echo = false){ $time_ago = human_time_diff( get_the_time('U',$post_id),, current_time('timestamp') ); if (strtotime($time_ago) < strtotime('1 day')) { $time_ago = "1"; } $days_ago = preg_replace("/[^0-9 ]/", '', $time_ago); $days_ago; $ratings_score = get_post_meta($post_id,'ratings_score',true); $hotness = $ratings_score / $days_ago; //here you store the hotness as post meta update_post_meta($post_id, 'hotness', $hotness); if ($echo) echo $hotness; return $hotness; } 

so you can now get and update the post hotness with this function by just passing it the post id.

next you create a hooked function to 'the_contentfilter hook which will update the post hotness every timethe_content` is called:

add_filter('the_content','update_hotness_filter'); function update_hotness_filter($content){ global $post; update_hotness($post->ID); return $content; } 

Second approach: scheduling an event

First create a function that will check if your event is scheduled or not the update accordingly:

add_action('wp', 'hotness_update_activation'); function hotness_update_activation() { if ( !wp_next_scheduled( 'daily_hotness_update' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'daily_hotness_update'); } } 

next you make sure to add an action hook to your even and a function that will run every day:

add_action('daily_hotness_update', 'daily_hotness_update_callback'); function daily_hotness_update_callback() { // here you get a list of posts to update the hotness for and you just // call update_hotness for each one ex: foreach ($posts as $p ) { update_hotness($p->ID); } } 

and that is it :).

If you ask me you should go with the simpler way which is the first approach.