Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

No idea how to title this so feel free to edit. I set out to create a "hotness" for my posts and to do this I used the $ratings_score which is stored as post_meta as a number +/- and the days ago of the post which I trimmed down to just the days. I divided the ratings_score by the days to get a number which reflects the hotness of the post. I ran this function inside the loop to get the result...

$time_ago = human_time_diff( get_the_time('U'), 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;
echo $hotness;

That works fine but what I really need to do is make this into a function in my functions.php and store $hotness as post_meta. I need it to constantly update itself aswell based on the changing of the days ago and $ratings_score. So then I can sort my loop by the $hotness meta_key. How can I do that?

share|improve this question
    
what is ratings_score made of? –  Bainternet Jun 3 '12 at 7:02
    
I should of added that I'm using wp_postratings which uses $ratings_score to store the +/- number of the rating. –  Pollux Khafra Jun 3 '12 at 12:16

1 Answer 1

up vote 4 down vote accepted
+50

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

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_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.

share|improve this answer
    
Thanks for this detailed answer. This works like a charm! –  Pollux Khafra Jun 3 '12 at 12:30

Your Answer

 
discard

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.