Skip Navigation

[Résolu] Show most popular posts based on post view count

Ce fil est résolu. Voici une description du problème et la solution proposée.

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.

Solution:
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:
https://github.com/wp-plugins/post-views-counter/blob/master/includes/counter.php

100% of people find this useful.

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.

Marqué : ,

This topic contains 16 réponses, has 2 voix.

Last updated by Christian Cox Il y a 4 années et 5 mois.

Assigned support staff: Christian Cox.

Auteur
Publications
#523184

Works like a charm.

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

Have a nice day,
Nicholas

#523188

Excellent, have a good one.