Show the most popular WordPress posts

This feature is only available in the Pro version.

As a website owner, you might want to display the most popular WordPress posts in a beautiful layout. Most popular posts have most number of visits.

1. Count number of visits

WordPress doesn’t have a built-in feature to count the number of visits for each post. You can choose one of two custom ways below to count visits of posts:

  • Use another plugin
    There are some WordPress plugins were created to implement this feature, one of them is WP-PostViews. Please install & activate that plugin.
  • Use custom coding
    In case you want to make it work without using extra plugin, please add this code to file functions.php in the theme’s folder (or install this plugin Code Snippets then add this code to the “Code” textarea):

    /** Content Views Pro - Enqueue script to count visits, support cache plugin */
    add_action( 'wp_enqueue_scripts', 'cv_theme_count_pviews_js', PHP_INT_MAX );
    function cv_theme_count_pviews_js() {
    	if ( !is_singular() ) {
    		return;
    	}
    
    	if ( !wp_script_is( 'jquery', 'done' ) ) {
    		wp_enqueue_script( 'jquery' );
    	}
    
    	// Must use "jquery-migrate"
    	wp_add_inline_script( 'jquery-migrate', 'jQuery(document).ready(function(){	jQuery.ajax({type:"GET",url:"' . admin_url( 'admin-ajax.php' ) . '",data:"post_id=' . $GLOBALS[ 'post' ]->ID . '&action=cv_count_pviews",cache:0});});' );
    }
    
    /** Content Views Pro - Count visits */
    add_action( 'wp_ajax_cv_count_pviews', 'cv_theme_count_pviews_php' );
    add_action( 'wp_ajax_nopriv_cv_count_pviews', 'cv_theme_count_pviews_php' );
    function cv_theme_count_pviews_php() {
    	if ( !isset( $_GET[ 'post_id' ] ) )
    		return;
    
    	$post_id = intval( $_GET[ 'post_id' ] );
    	if ( $post_id > 0 ) {
    		update_post_meta( $post_id, 'views', (int) get_post_meta( $post_id, 'views', true ) + 1 );
    		exit();
    	}
    }
    
    /** Content Views Pro - add default visit count 1 to all posts */
    add_action( 'admin_head', 'cvp_theme_add_default_visit_count' );
    function cvp_theme_add_default_visit_count() {
    	if ( !get_option( 'cvp_theme_add_visit_count' ) ) {
    		global $wpdb;
    		$wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id,meta_key,meta_value)
      SELECT ID, 'views', 1
      FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status = 'publish'" );
    
    		add_option( 'cvp_theme_add_visit_count', 1, '', 'no' );
    	}
    }
    

2. Create a View to show popular posts

After finishing above, here are some easy steps to show most popular WordPress posts with Content Views Pro:

  • Open your homepage (or any another post or page) to start counting
  • In Admin area, click Content Views > Add New to start.
  • Select the Sort by option in the Filter Settings tab, Advance section of the View
  • In the “Sort by” group, click Add New button, then select options as below image:
     
    CVP - show most popular posts

Now you can add that View to where you want to show the most popular posts.

Notice: At the beginning, the View might return no posts. Because of there are no posts have the views custom field. When someone visits your posts, the views custom field will be created and updated, and you will start seeing the popular posts.

Best regards,

Scroll to Top