How to 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. There are 2 possible ways 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 an 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):

    /** Enqueue script to count views, 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});});' );
    }
    
    /** Count views */
    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();
    	}
    }
    

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:

  • 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 will 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,