Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #5625
    hyest
    Participant

    Hello,

    I’m using your plugin with woocommerce. It is counting and doing good. Is there by chance anyway I can sort woocommerce products by page views (most popular based on amount of page views)?

    Maybe something simple like passing it through a url like: http://example.com/?v_sortby=views&v_orderby=desc

    Any help would be appreciated

    #5626
    Bartosz
    Keymaster

    Yes, of course.

    This plugin extends the query available orderby parameters by adding “post_views”. WooCommerce product is just a “product” post type so you can run a standard WP_Query with this new parameter.

    Like this:

    $query = new WP_Query(array('post_type' => 'product', 'orderby' => 'post_views', 'order' => 'DESC'));

    Get queries, as you proposed above, should interpret ‘orderby=post_views’ properly as well. Theoretically :)

    #8839
    inphilly
    Participant

    I tried to use the above code adapted for a woocommerce sort and it did not work. It just sorts by the default. I am using the FacetWP plugin. Here is my code. It seems almost as if it cannot find the post_views field at all. I did add ‘post_type’ => ‘product’ to the query_args as well, and that did not work. I would appreciate your help getting this working.

    function my_facetwp_sort_options( $options, $params ) {

    $options[‘popularity_new’] = array(
    ‘label’ => ‘Popularity’,
    ‘query_args’ => array(
    ‘orderby’ => ‘post_views’,
    ‘order’ => ‘DESC’, // descending order
    )
    );

    $options[‘price_high_to_low’] = array(
    ‘label’ => ‘Price: High to Low’,
    ‘query_args’ => array(
    ‘orderby’ => ‘meta_value_num’, // sort by numerical custom field
    ‘meta_key’ => ‘_price’, // required when sorting by custom fields
    ‘order’ => ‘DESC’, // descending order
    )
    );

    $options[‘price_low_to_high’] = array(
    ‘label’ => ‘Price: Low to High’,
    ‘query_args’ => array(
    ‘orderby’ => ‘meta_value_num’, // sort by numerical custom field
    ‘meta_key’ => ‘_price’, // required when sorting by custom fields
    ‘order’ => ‘ASC’, // ascending order
    )
    );

    return $options;
    }
    add_filter( ‘facetwp_sort_options’, ‘my_facetwp_sort_options’, 10, 2 );

    I am using WordPress 4.3.1 and Woocommerce 2.4.7.

    #8846
    Bartosz
    Keymaster

    I can’t help you with FacetWP related question I think. But the function above should work – WooCommerce product is just a post type. How did you test it?

    #8848
    inphilly
    Participant

    This is for a sort widget. The FacetWP part works fine, and the rest works fine, except for the query on the “post_views”. The query itself is somehow wrong… I have tried it with field name “_post_views” and it still does not work. So this is not a FacetWP issue, but a matter of getting the query_args correct for post_views.

    #8849
    inphilly
    Participant

    I think possibly you are storing your data on another table?

    #8852
    Bartosz
    Keymaster

    Yes, we’re storing the post_views in separate table, for performance and optimization reasons.

    But we’ve created sql joins (using WP native filter hooks) for the WP_Query – whenever you use post_views as orderby parameter you get the correct result and any WP_Query, including tax_query and meta_query is possible.

    #8860
    inphilly
    Participant

    Does it matter if counter mode is PHP or Javascript?

    #8861
    inphilly
    Participant

    And does it matter if I removed all text from the Post Views Label?

    #8862
    Bartosz
    Keymaster

    If you use caching – then yes, you have to switch to JS.
    But in terms of query, does not matter at all.

    #8866
    inphilly
    Participant

    I have the developer of FacetWP helping me out with this. He looked at the query that is being run from the console and said the only interesting thing he saw is that there are two GROUP BY’s, and said maybe I could run it by you…

    Here is the code: (He replaced the post id’s with 1,2,3,4,5 for brevity)

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
    INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    LEFT JOIN wp_post_views pvc ON pvc.id = wp_posts.ID AND pvc.type = 4
    WHERE 1=1
    AND wp_posts.ID IN (1,2,3,4,5)
    AND ( ( wp_postmeta.meta_key = ‘_visibility’ AND CAST(wp_postmeta.meta_value AS CHAR) IN (‘visible’,’catalog’) ) )
    AND wp_posts.post_type = ‘product’
    AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’)
    GROUP BY wp_posts.ID, wp_posts.ID
    ORDER BY pvc.count DESC, wp_posts.ID DESC
    LIMIT 0, 32

    Do you have any thoughts on this?

    #8891
    inphilly
    Participant

    I disabled the facetwp sort and tried this with just the woocommerce sort. It did not work at all. Have you ever tried this with WooCommerce sorting? If so, I would love if you could post some code that works for you.

    #8900
    Bartosz
    Keymaster

    Try the just released 1.1.1

    It should handle the double groupby. You will also have post_views in returend array of post objects. So even if the direct query fails you’l be able sort that on php level.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.