Skip Navigation

[Resolved] Show most popular posts based on post view count

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the post view count generated from a 3rd party plugin (dfactory's Post Views Counter) to sort a View by post popularity. I would like to use this for the Product post type created by WooCommerce.

Unfortunately the view count generated by this plugin is not stored in the postmeta table in a way that Views can access directly. Therefore it is necessary to add some custom code that updates a separate custom field whenever the post view count is updated by the counter plugin. Then this custom field can be utilized by Views for sorting using the standard View editor.

1. Create a new custom field group for products. Add a custom number field called "Product View Count". Make this field required, with Number validation, and make the default value 0. (see attached field.png in the post below)
2. Edit this field group to be used only on the Products post type.
3. Make sure your count settings in Settings > Post Views Counter include the Products post type.
4. Add the following code to your functions.php file:

add_action( 'pvc_after_count_visit', 'update_toolset_view_count' );
function update_toolset_view_count ( $post_id ) {
  $post_type = 'product';
  // only update the post view count for products
  if ($post_type == get_post_type($post_id)) {
    $view_count = get_post_meta($post_id, 'wpcf-product-view-count', true);
    $view_count = $view_count ? $view_count : 0;
    // update and increment the view count for this product
    update_post_meta($post_id, 'wpcf-product-view-count', (intval($view_count) + 1));

5. Navigate to one of your products in wp-admin. You should see your custom field here with a value of "0" by default. If you are okay with starting from 0, you don't need to change this value. If you want to start calculating from the view count as it exists already, you must copy + paste that value from the front-end of your site.
6. Open a separate incognito browser window. Navigate to that product on the front-end of your site.
7. Return to your product in the wp-admin area and refresh the page. You should now see the updated view count number.
8. Create a new product view. In the sorting options, you should now be able to select your product view count field and sort based on that value as a number.

Relevant Documentation:

100% of people find this useful.

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 16 replies, has 2 voices.

Last updated by Christian Cox 5 years, 9 months ago.

Assigned support staff: Christian Cox.


Works like a charm.

I wish I could by you a cup of coffee for your help.

Have a nice day,


Excellent, have a good one.