How to change the order of posts by number of view

2019-06-13 02:23发布

I'm making a drawing website using WordPress and allowing my users to draw and post their drawings as pinned posts

enter image description hereenter image description here

Now I want to make the theme I'm using to change the order of the displayed posts to be displayed by number of views or comments not by the date order

Where can I find the code responsible for the posts displaying order in the theme of WordPress.

标签: php wordpress
1条回答
\"骚年 ilove
2楼-- · 2019-06-13 03:01

If you want to changer the display order then you can use pre_get_posts action.

To order post by comment count

function wh_post_display_order_comment($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('orderby', 'comment_count');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_comment');


To order post by view

By default WordPress does not have any option to short post by view so you have to use a little trick

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($postID) {
    $count_key = 'whpp_track_post_views';
    $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);

Now that we have set our logic for view so we 'll be shorting it

function wh_post_display_order_view($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('meta_key', 'whpp_track_post_views');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_view');


To order post by both view and comment count

If you want to orderby both the score then again we have to apply a small trick, as there is no default option in WordPress. First we'll count the comment and we'll add a small weightage and add the total view count and keep it is a different meta field and then we'll sort the post on that key.

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($postID) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($postID, $count_key, true);
    //retriving total comments
    $comments_count = wp_count_comments($postID);
    $total_comment = $comments_count->total_comments;

    $comment_point = 2; //change the number with your desired weightage
    $comment_score = $total_comment * $comment_point;
    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);
    }
    update_post_meta($postID, 'whpp_view_comment_score', ($count + $comment_score));
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);


function wh_post_display_order_view($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('meta_key', 'whpp_view_comment_score');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'DESC');
    }
}

add_action('pre_get_posts', 'wh_post_display_order_view');

Please Note : Adding extra weight to comment is not mandatory, but if reader will realy like the post then only they 'll give the comment.

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Hope this helps!

查看更多
登录 后发表回答