按观看次数排序产品 [英] Sort products by most viewed

查看:42
本文介绍了按观看次数排序产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个已经计算产品浏览量并将其存储在名为"mwb_wpr_data"的表中的插件.

I'm using a plugin that already count product views and store it in a table called 'mwb_wpr_data'.

列出产品视图的查询是:

The query to list the product views is:

SELECT DISTINCT('productid') FROM 'mwb_wpr_data' WHERE 'action' = 'view'

productid字段是Woocomerce产品的FK.

The productid field is a FK to Woocomerce products.

如何修改Woocommerce的默认排序方式,以便它将根据"mwb_wpr_data"表中的大多数视图顺序显示产品?

How can I modify the Woocommerce default sorting, so it will display products by order of most views based in the table 'mwb_wpr_data'?

使用插件Post View Counter的当前代码:

Current code using the plugin Post View Counter:

add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
    if ( 
        $query->is_main_query() && 
        ( $query->is_home() || $query->is_archive() || $query->is_search() )
    ) {
            $query->set('suppress_filters', 'false');
            $query->set('orderby', 'post_views');
            $query->set('order', 'DESC');
    }
}

推荐答案

据我说,您设置了正确的pre_get_post,但是问题在于您的if条件.

According to me you have set the correct pre_get_post but problem is in your if condition.

您已为每个用户设置了current_user_can,这是不正确的过滤器,因此,如果您不是以管理员身份登录,则查询将不起作用.

You have set the current_user_can which not correct filter is for every user so if you are not login in with administrator role than your query will not work.

current_user_can('administrator')

将其从条件中删除.

add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
    if ($query->is_main_query() && ( $query->is_home() || $query->is_archive() || $query->is_search() )
    ) {
            $query->set('suppress_filters', 'false');
            $query->set('orderby', 'post_views');
            $query->set('order', 'DESC');
    }
}

这篇关于按观看次数排序产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆