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.

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.

Our next available supporter will start replying to tickets in about 0.22 hours from now. Thank you for your understanding.

This topic contains 16 replies, has 2 voices.

Last updated by Christian Cox 4 years, 6 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#521635

Hey I need to display the most popular posts based on it's views. I would love to use toolset views to to it.

I am using the following plugin to count the views:
https://wordpress.org/plugins/post-views-counter/

The Plugin does not store views in postmeta, but it extends WP_query with a new orderby parameter..

This thread might be relevant to you
https://wordpress.org/support/topic/how-to-sort-by-post-views-using-meta-value/
https://toolset.com/forums/topic/popular-posts/

How do I make use of it inside a toolset view?

Regards,
Nicholas

#521699

Hi, you won't be able to select this order option in your View's settings but I can provide some code that will enable sorting on post_views. Add the following code to your theme's functions.php file:

// order by post views:
add_filter( 'wpv_filter_query', 'sort_by_post_views', 1000, 3 );
function sort_by_post_views( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 123 ) {
        $query_args['orderby'] = 'post_views';
        $query_args['order'] = 'DESC';
    }
    return $query_args;
}

Replace 123 with the ID of the View you would like to sort. If you want to sort in ascending order, change 'DESC' to 'ASC'. Note that this will override any sorting settings you include in your View.

#521724
Search_MockUpNew.jpg

Thank you very much Christian.

So this means I can't use this in a view where I'd like to use more than one filter?

I.e.: Sort by Relevance - Popularity - Date added? (see image)

#521731

...in a view where I'd like to use more than one filter? I.e.: Sort by Relevance - Popularity - Date added? (see image)
Just to be clear - sorting and filtering are different things. These are all sorting options, not filters. It's perfectly fine to use the approach I described before alongside search filters. If you want to add a taxonomy filter or a custom field filter to this search, no problem.

Can you explain in a bit more detail how this new search mockup would be accomplished? Views does allow users to choose how a View is sorted on the front-end, but I don't know that your post_views number will be available here. The alternative option is to show your results in a table, which users can sort on their own by clicking the table headers.

Edit - updated this to reflect information from https://toolset.com/documentation/user-guides/allowing-visitors-to-sort-the-front-end-results/

#521744

Whats the difference between sorting and filtering? Is there some documentation about that?

I thought about setting up filter radio buttons that I style as buttons (shown in the screenshot). These buttons can then be clicked and the results will update according to the radio button selected.

I'd love to set up radio buttons that filter the
- most recently added
- oldest
- most popular/most viewed
...
posts.
I'd also like to use custom fields with it.

How would you approach this?

#521757

I'd love to set up radio buttons that filter the
- most recently added
- oldest
- most popular/most viewed

These are sorting options, not filters. The difference between sorting and filtering is that sorting changes the order of your results. It does not change which results will be included in the full results list. Filters do not change the order results appear in the list of results. Filters determine which results will be displayed.

As an example, think about a spreadsheet in Excel. If you click a table header, the results are sorted by the data in this column. The results don't change, they are only re-ordered. A filter would only show the rows of results that match your filter. It does not change the order of the results that are displayed.

I'd also like to use custom fields with it.
I'm not sure I understand what you mean. Do you want to sort by custom fields, or filter by custom fields?

More information about filtering here:
https://toolset.com/documentation/user-guides/front-page-filters/

I found some more information about user-specified sorting here:
https://toolset.com/documentation/user-guides/allowing-visitors-to-sort-the-front-end-results/
So it's possible to allow users to sort in some ways on the front-end, but post_views won't be available here for your users to select for sorting.

#521766

Thanks so much Christian.
Got it.

Great documentation. Exactly what I was looking for.

Maybe use a different plugin for post views sorting or won't this be possible at all?

#522096

Before switching plugins, I'd ask the support team for your current view count plugin if they offer a way to "hook" into the view count update event. If so, I could provide some PHP code that would set up a custom field on your post type, and update that field whenever the view count is updated. This way Views would have access to the view count information for each post and could then sort results with that view count. The field would appear in the regular sorting GUI and work like any other sort option.

If they cannot provide such an event hook, then we could try to set up your sorting options as custom inputs in your filter controls. Based on the selection in these custom inputs, I could provide some PHP code that will enforce the correct sort order. One drawback to this approach is that any time you want to change the available sorting options, you must manually add the option to your filter controls, and you must manually update the PHP code that supports each option. Another drawback is that you cannot manage the fields through the standard Views sorting GUI.

#522156

Wow. That sounds great. Thank you for looking into this.

Right now I am using this plugin https://wordpress.org/plugins/post-views-counter/

If this is not working I'd probably switch over to https://wordpress.org/plugins/baw-post-views-count/. However this one is not optimal for my needs.

Regards,
Nicholas

#522161

Hi, I think there was a miscommunication, sorry. I wanted to clarify that contacting the plugin's support team would be your responsibility, since you are really their customer. Ask if they offer a way to "hook" into the view count update event using PHP, and a code sample that shows how it can be done. Please let me know what you find out.

#522686

Sorry Christian my bad.
Thank you for clarifying it.

I'll keep you updated about their reply.

https://wordpress.org/support/topic/meta-keys-created-in-the-database/#post-9116135

#522739

Hey Christian .
I just received a reply from their support.

https://wordpress.org/support/topic/meta-keys-created-in-the-database/#post-9118458

"For example using pvc_after_count_visit action hook being fired after the view has been counted (increased).

That’s how it looks in the code:
do_action( 'pvc_after_count_visit', $id );
where $id is the post ID

To call it just make use of the WP native add_action function and pvc_after_count_visit hook."

Would this work with toolset? If yes how?

Regards,
Nicholas

#523053

Okay great, we can work with this and I can provide some code. Which of your post types will require this sorting? If any of them are custom post types, please provide the slugs for each one.

#523100

Thank you so much Christian.
It's just the product post type.

#523170
field.png

Ok great. Here's how to integrate it.
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)
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.

Let me know how it goes!