使用此选项时,我尝试按自定义字段值对结果排序:
// Search product distance
foreach( $productArrayShop as $id ) {
$pdistance = $product_obj[\'current_distance\']=get_post_meta($id,\'current_distance\');
echo \' Distance = \'.$pdistance[0].\', \';
$arrayDistance[] = $pdistance[0];
}
//echo \'<b> Founded \'.count($arrayDistance).\' distance</b> \';
// Orderby distance
foreach ($arrayDistance as $d)
{
$productd = array
(
\'post_type\' => \'product\',
\'posts_per_page\'=> -1,
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'current_distance\',
\'fields\' => \'ids\',
\'meta_query\' => array
(
array(
\'key\' => \'current_distance\',
\'value\' => $d,
\'type\' => \'NUMERIC\',
\'compare\'=> \'<\'
)
)
);
//Get product id
$product_post = get_posts($productd);
//echo $product_post[0].\', \';
//echo count($product_post);
$a[] = $product_post[0];
}
//echo \' Founded \'.count($a).\' ID\';
$resultD = implode(",", $a);
echo \' ID of product \'.$resultD.\', \';
结果是:距离=62.717,距离=726.727,产品ID是,8132281322,并且只有第一个产品出现在屏幕上,这是有意义的,因为相同的ID。。。我不明白为什么。
的转储
$a[]: array(3) { ["map"]=> string(3) "yes" [0]=> int(81322) [1]=> int(81322) }
为什么我有两个相同的ID 8132281322?
如果我不使用查询排序,我的两个产品会显示在屏幕上(当然没有排序)。
跟踪时:
foreach ( $productArrayShop as $id ) {
$pdistance = $product_obj[\'current_distance\']=get_post_meta($id,\'current_distance\');
echo \' Distance = \'.$pdistance[0].\', \';
$arrayDistance[] = $pdistance[0];
}
我的两个产品有两个距离。
工作代码为:
$productd = array(
\'post_type\' => \'product\',
\'posts_per_page\'=> -1,
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'current_distance\',
\'post__in\' => $productArrayShop // Here we get all the posts by the IDs with the right order.
);
$product_post = get_posts($productd);
foreach($product_post as $p) {
$arrp[] = $p->ID;
}
$result = implode(",", $arrp);
echo \' ID of product \'.$result.\', \';
// View list of product based on road distance of user position
echo do_shortcode(\'[products ids="\' . $result . \'" per_page="8" columns="4" pagination="true" orderby="meta_value" order="ASC"]\');
感谢@Shibi对我的帮助!
最合适的回答,由SO网友:Shibi 整理而成
据我所知$productArrayShop
是所有产品ID的数组。
所以你首先不需要所有的距离。获取距离的点是什么?您只需要所有产品ID,并通过meta_value
.
顺便说一句,这个订单应该在第一个循环中,而不是重新查询订单。
/* So we don\'t need this. */
/*
foreach ( $productArrayShop as $id )
{
$pdistance = $product_obj[\'current_distance\']=get_post_meta($id,\'current_distance\');
echo \' Distance = \'.$pdistance[0].\', \';
$arrayDistance[] = $pdistance[0];
}
*/
/**
* We dont need to loop for each distance we have already array for all the products IDs its all we need.
*/
$productd = array
(
\'post_type\' => \'product\',
\'posts_per_page\'=> -1,
\'order\' => \'ASC\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'current_distance\',
\'post__in\' => $productArrayShop // Here we get all the posts by the IDs with the right order.
);
$product_post = get_posts($productd);
// Then do something with the products...
print_r($product_post);
/**
* You can loop to get each post object
*/
foreach($product_post as $p) {
echo $p->ID;
}
还有一件事可以帮助您理解为什么您会得到相同的ID。这是因为你有两个距离,在第二个循环中,你选择低于第一个距离的柱子,然后选择低于第二个距离的柱子。
当然,你也会得到同样的职位。因为如果它符合第一个条件,它也会符合第二个条件。因为你选择了两次帖子。然后你用ASC(第一个更低的)来排序,所以相同的帖子在索引0中。
Woocommerce [products]
shortcode custom orderby
第一步,您需要向函数中添加过滤器。php,用于检查是否将自定义orderby值传递给短代码,并将查询参数设置为所需的元值。
add_filter(\'woocommerce_shortcode_products_query\', \'woo_products_sc_distance_order\', 10, 3);
function woo_products_sc_distance_order($args, $atts, $type) {
if ($atts[\'orderby\'] == "distance") {
$args[\'orderby\'] = \'meta_value\';
$args[\'meta_key\'] = \'current_distance\';
}
return $args;
}
然后用orderby距离调用快捷码
echo do_shortcode(\'[products ids="\' . $result . \'" per_page="8" columns="4" pagination="true" orderby="distance" order="ASC"]\');
和基本用法:
[products orderby="distance"]