• i want to sort posts (sort by views) using this function

    i am currently using this code in function.php to get post views count

    <?php
    function getPostViews($postID){
        $count_key = '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';
    }
    function setPostViews($postID) {
        $count_key = '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);
        }
    }
    ?>

    i want to integrate post_views_count to sort posts with this code

    $params = $_GET;
    unset($params['orderby']);
    unset($params['order']);
    unset($params['v_orderby']);
    unset($params['v_sortby']);
    $url = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
    $url = strtok($url, '?');
    $url =$url.'?'.http_build_query($params);
    &orderby=comment_count&order=desc
    &v_sortby=views&v_orderby=desc
    $active = "date";
    if ($_GET["orderby"]=="comment_count"){
        $active = "comment";
    }
    if ($_GET["v_sortby"]=="views"){
        $active = "views";
    }

The topic ‘sort posts (sort by views) using post_views_count’ is closed to new replies.