Rating based off of number of comments or views

Is it possible to have some sort of rating feature based off of x amount of comments or views?

Example:

Instead of saying 200 views, have it say:

“Hot”

400 views, have it say:

“Very Hot”

something similar to a if else statement

value=200
Echo “hot”
if else
echo “cold”

Answer

I would not take visits into account: To get these numbers you have to connect to an external table, or worse write into the WordPress tables on each request.

Use comments, that is built-in. Here is a sample code that adjusts itself to your comment development:

add_filter( 'the_content', 'wpse_78513_hotness' );

function wpse_78513_hotness( $content )
{
    static $average = FALSE;

    static $global_stats = NULL;

    if ( FALSE === $average )
        $average = round( wp_count_posts()->publish / wp_count_comments()->approved );

    $current_comments = wp_count_comments( get_the_ID() )->approved;

    $hotness = 'lukewarm';

    if ( $current_comments >= ( $average * 1.5 ) )
        $hotness = 'hot';

    if ( $current_comments <= ( $average / 2 ) )
        $hotness = 'cold';

    $stats = "<p class='hotness'>Hotness: $hotness</p>";

    return $stats . $content;
}

It counts posts and comment on the first call and stores these data in an internal static variable to save time on further calls. Then it takes the comment count for the current post and calculates how much it differs from the average.

You can and should extend it. It is a guide, not a complete solution.

Attribution
Source : Link , Question Author : Shawnshh , Answer Author : fuxia

Leave a Comment