按评级排序在wp_Query中不起作用

时间:2015-09-20 作者:Francesco

我需要在wp\\U查询循环中按评级对产品进行排序:

这是我的代码:

switch($ordering){
    case \'default\':
        $meta_key = \'\';
        $order = \'asc\';
        $orderby = \'menu_order title\';
        break;
    case \'popularity\':
        $meta_key = \'\';
        $order = \'desc\';
        $orderby = \'total_sales\';
        break;
    case \'low_to_high\':
        $meta_key = \'_price\';
        $order = \'asc\';
        $orderby = \'meta_value_num\';
        break;
    case \'high_to_low\':
        $meta_key = \'_price\';
        $order = \'desc\';
        $orderby = \'meta_value_num\';
        break;
    case \'newness\':
        $meta_key = \'\';
        $order = \'desc\';
        $orderby = \'date\';
        break;
    case \'rating\':
        $meta_key = \'\';
        $order = \'desc\';
        $orderby = \'rating\';
        break;
}

$args_products_sorting = array(
    \'post_type\' => \'product\',
    \'posts_per_page\' => -1,
    \'orderby\' => $orderby,
    \'order\' => $order,
    \'meta_key\' => $meta_key
);
错误在哪里?

如果我在url中设置?orderby=rating 它工作正常。

谢谢

5 个回复
SO网友:Shamar Kellman

您应该使用\'orderby\' => \'meta_value_num\'\'meta_key\' => \'rating\' 由WordPress编解码器指定。使用meta_value_num 假设您的评分是数值

参考号:Orderby Parameters

SO网友:borisdiakur

似乎有很多元密钥可以用来订购(here 是我找到的列表)。然而,我找不到任何与评级相关的内容。我搜索了rating 在WooCommerce源代码中找到以下行:includes/abstracts/abstract-wc-product.php#L1087

下面是在使用WooCommerce PHP REST API客户端的环境中最终对我起作用的内容:

function getProducts(WP_REST_Request $request) {
    global $wc_api_client;

    $category = $request->get_param(\'category\');
    // $filters = $request->get_param(\'filters\');
    $orderby = $request->get_param(\'orderby\');

    $args = null;

    $wc_query = new WC_Query();
    switch ( $orderby ) {
        case \'date\':
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'date\',
                \'filter[order]\' => \'DESC\'
            );
            break;
        case \'price\':
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'meta_value_num\',
                \'filter[order]\' => \'ASC\',
                \'filter[orderby_meta_key]\' => \'_price\'
            );
            break;
        case \'price-desc\':
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'meta_value_num\',
                \'filter[order]\' => \'DESC\',
                \'filter[orderby_meta_key]\' => \'_price\'
            );
            break;
        case \'popularity\':
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'meta_value_num\',
                \'filter[order]\' => \'DESC\',
                \'filter[orderby_meta_key]\' => \'total_sales\'
            );
            break;
        case \'rating\':
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'meta_value_num\',
                \'filter[order]\' => \'DESC\',
                \'filter[orderby_meta_key]\' => \'_wc_average_rating\'
            );
            break;
        default:
            $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category
            );
    }

    try {
        $raw = $wc_api_client->products->get(\'\', $args);
        return $raw[\'products\'];
    } catch (Exception $e) {
        return new WP_Error(\'no_products\', \'Couldn’t find any products\', array(\'status\' => 404));
    }
}

SO网友:user2462948

我用过这样的东西,而且很管用

switch ($order_by){
                case \'price\':
                    $args[\'orderby\'] = \'meta_value_num\';
                    $args[\'meta_key\'] = \'_price\';
                    $args[\'order\'] = \'asc\';
                    break;

                case \'price-desc\':
                    $args[\'orderby\'] = \'meta_value_num\';
                    $args[\'meta_key\'] = \'_price\';
                    $args[\'order\'] = \'desc\';
                    break;

                case \'rating\':
                    $args[\'orderby\'] = \'meta_value_num\';
                    $args[\'meta_key\'] = \'_wc_average_rating\';
                    $args[\'order\'] = \'desc\';
                    break;

                case \'popularity\':
                    $args[\'orderby\'] = \'meta_value_num\';
                    $args[\'meta_key\'] = \'total_sales\';
                    $args[\'order\'] = \'desc\';
                    break;
            }

SO网友:Iris Nguyen

试试这个。使用WC\\u Shortcode\\u产品生成WC循环,默认WC车间页面使用排序参数

for($i=1;$i<=$num;$i++){
    $atts = array_merge(array(
        \'columns\' => $columns,
        \'orderby\' => $order_by,
        \'order\' => $sort_by,//rating , population , price high to low ,....
        \'rows\' => $rows,
        \'page\' => $i,
    ));
    $shortcode = new WC_Shortcode_Products($atts, \'recent_products\');
    echo $shortcode->get_content();
}

SO网友:softnwords

将此代码用于参数

$args = array(
            \'post_type\' => \'product\',
            \'orderby\'   => $args = array(
                \'filter[limit]\' => 6,
                \'filter[category]\' => $category,
                \'filter[orderby]\' => \'meta_value_num\',
                \'filter[order]\' => \'DESC\',
                \'filter[orderby_meta_key]\' => \'_wc_average_rating\'
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\'  => \'product_cat\',
                    \'field\'     => \'id\',
                    \'terms\'     => $category
                )
            ),
            \'posts_per_page\' => $product_number
        );

相关推荐