Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter brandenfeld

    (@brandenfeld)

    $sum = $all_views; … is unnecessary.

    <?php
    $meta_key = 'views';
    $all_views=$wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
    
    $post_views = intval( get_post_meta( get_the_ID(), 'views', true ) );
    echo round(($post_views * 100/$all_views),2). " % of all views";
    ?>
    Plugin Author Lester Chan

    (@gamerz)

    There is a function to get all views in the plugin

    $all_views = get_totalviews( false );

    Thread Starter brandenfeld

    (@brandenfeld)

    Thanks. Danke.

    Thread Starter brandenfeld

    (@brandenfeld)

    Hi, what I’m trying to do now is a visible Ranking.

    At the end of all posts I want to show something like: 22 views – 0.45% (Platz 198)

    I have more than 700 entries (Books). Is this really possible after all?

    interesting… get the % of current post against every post… i’d like to know if this is possible too ??

    Thread Starter brandenfeld

    (@brandenfeld)

    PHP-amateur-solution, first found here: https://wordpress.stackexchange.com/questions/164012/post-rank-by-views

    <?php
    
    global $post;
    $post_id = $post->ID;
    
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'meta_key' => 'views',
            'orderby' => 'meta_value_num',
            'ignore_sticky_posts' => 1,
            'posts_per_page' => '9999',
    
        );
    
    $my_query = new WP_Query( $args );
    
    if ( $my_query->have_posts() ) :
    
    $postnum = 1;
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
    if ($post->ID == $post_id) { echo "Rank: " .$postnum. "."; } else { echo ""; }
    
    $postnum++; endwhile; endif; wp_reset_postdata(); ?>

    I can’t explain, but it works ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘% of total views’ is closed to new replies.