• tienduydng

    (@tienduydng)


    Hi everybody!.
    I have a problem with try to get popular post by views.

    //////update post view
    function wp_set_post_views($postID) {
        $count_key = 'wpb_post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
        }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    }
    //To keep the count accurate, lets get rid of prefetching
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    //show view
    function wp_get_post_views($postID){
        $count_key = 'wpb_post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            return "0 view";
        }
        return $count.' views';
    }
    //////////this is my code to get list by views:
    $arg = array(
     "post_type" => "product",
     "posts_per_page" => 10,
     "meta_key" => "wpb_post_views_count",
     "orderby" => "meta_value_num",
     "order" => "DESC",
    );
    $query = new wp_query($arg);
    if($query->have_posts()) :
     while($query->have_posts()) : $query->the_post();
      the_title();
      echo '('.wp_get_post_views(get_the_ID()).')';
     endwhile;
    endif;
    ?>

    But the result is not correct. The list is still sort by post ID.
    where is my error. How can i fix it. Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter tienduydng

    (@tienduydng)

    no one can help me

    Clarion Technologies

    (@clarionwpdeveloper)

    Hello tienduydng,

    Are you getting the count of views ?

    Clarion Technologies

    (@clarionwpdeveloper)

    As I have used this in my theme and its working perfectly.Can you please paste your single.php file code here so that I can help you if anything missing over their.

    Thread Starter tienduydng

    (@tienduydng)

    oh. Thanks Clarion for your reply.
    function wp_set_post_views($postID) {

    }
    //To keep the count accurate, lets get rid of prefetching
    remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);
    //show view
    function wp_get_post_views($postID){

    }
    I put two above functions in function.php.

    and below code i put in sidebar.php
    <?php
    //////////this is my code to get list by views:
    $arg = array(
    “post_type” => “product”,
    “posts_per_page” => 10,
    “meta_key” => “wpb_post_views_count”,
    “orderby” => “meta_value_num”,
    “order” => “DESC”,
    );
    $query = new wp_query($arg);
    if($query->have_posts()) :
    while($query->have_posts()) : $query->the_post();
    the_title();
    echo ‘(‘.wp_get_post_views(get_the_ID()).’)’;
    endwhile;
    endif;
    ?>
    In index.php I get_sidebar();
    So where is problem. Thanks.

    Thread Starter tienduydng

    (@tienduydng)

    Let me explain more detail.
    The result show:

    product title 1 (2 view)
    product title 2 (1 view)
    product title 3 (5 view)
    product title 4 (8 view)
    product title 5 (3 view)

    But i need result.
    product title 4 (8 view)
    product title 3 (5 view)
    product title 5 (3 view)
    product title 1 (2 view)
    product title 2 (1 view)

    I just want to sort by views

    Clarion Technologies

    (@clarionwpdeveloper)

    Hello tienduydng,

    I have implemented Your code in my Theme also but is working as you expected Its strange I am also confused why it is not working on your side.

    Thread Starter tienduydng

    (@tienduydng)

    Thanks Clarion for your words with my heart!
    I found my problem. Because i use “pre_get_post”
    $query->set(‘orderby’,’modified’);
    I am so crazy!!
    Thanks Clarion again. Nice to meet you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get popular custom post type post by views’ is closed to new replies.