NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate
Web Hosting Canada

Last modified : Sep 11 2022

Estimated reading time : 2 minutes, 47 seconds - 123 words

With this tutorial, we will help you sort your post types by their popularity. First thing we need to do is a meta post (views) which will all you go get each post type’s number of views.

We will call it via_views_count which will the meta key to get with our query.

///////////////////////////////////////
// Popular Posts By views for Posts
///////////////////////////////////////

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

///////////////////////////////////////
// Tracks Views
///////////////////////////////////////
function via_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
	global $post;
$post_id = $post->ID;    
}
via_post_views($post_id);}
add_action( 'wp_head', 'via_track_post_views');

///////////////////////////////////////
// Get Posts Views
///////////////////////////////////////
function via_get_post_views($postID){
$count_key = 'via_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 Vue";
}
return $count.' Vues';
}

We will create the Views column displayed in the post table which will return each post’s views.

///////////////////////////////////////
// Admin Columns display posts Views
///////////////////////////////////////

add_filter('manage_posts_columns', 'via_column_views');
add_action('manage_posts_custom_column', 'via_custom_column_views',5,2);
function via_column_views($defaults){
    $defaults['via_post_views'] = __('Vues');
    return $defaults;
}
function via_custom_column_views($column_name, $id){
        if($column_name === 'via_post_views'){
        echo via_get_post_views(get_the_ID());
    }
}

Sort Post Types by Popularity

Here’s the query to use to sort your post types by popularity.

You can use your HTML to display the results. In this tutorial, it’s only an hyperlink.

Replace post by the name of your Post Type.

<?php 
$popularpost = new WP_Query( 
array( 
'post_type'                 => 'post', 
'post_status'               => 'publish', 
'posts_per_page'            => 9, 
'meta_key'                  => 'via_views_count', 
'orderby'                   => 'meta_value_num', 
'order'                     => 'DESC' ) ); 
while ( $popularpost->have_posts() ) : $popularpost->the_post(); 
?>
<?php the_title();?>
<?php endwhile; ?>	
<?php wp_reset_query(); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>