How to show most popular WordPress posts?

This feature is only available in the Pro version.

A popular post has many number of views (visits). As a website owner, you might want to display the most popular WordPress posts in a beautiful layout.
WordPress doesn’t have a built-in feature to count the number of views of each post. There are some WordPress plugins were created to implement this feature, one of them is WP-PostViews.

Here are some easy steps to show your most popular WordPress posts in a beautiful layout, with Content Views Pro:

  • Install & activate the WP-PostViews plugin (*)
  • In Admin area, click Content Views > Add New to start showing the most popular posts.
  • 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

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.


Notice (*): 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();
	}
}

Best regards,