【发布时间】:2021-03-13 15:38:03
【问题描述】:
我的 wordpress 数据库中有大约 2000 个 woocommerce 产品,每个产品都有一个名为 place 的 ACF 选择字段,其值为 local 或 regional .
我的目标是按地点元字段对每个产品进行分组,所以我认为以下代码可以完成这项工作:
class Search
{
public function __construct()
{
add_action("pre_get_posts", [$this, "filterProducts"]);
add_filter('posts_groupby', [$this, 'groupByFilter'] );
}
public function filterProducts($query){
// ... code
// This where i alter que query
// .. code
return $query;
}
public function groupByFilter($groupby)
{
global $wpdb;
return $wpdb->postmeta . ".meta_key = 'place'";
}
}
但是,这不起作用,只输出一种产品,而预期的结果是:
-- 本地
- 产品
- 产品
-- 地区性
- 产品
- 产品
我知道每个产品的 foreach 都是一个解决方案,但出于性能原因,我希望通过主查询直接处理它。
注意:如果有帮助,我会使用木材,并且在 search.php 文件中我有这段代码:
$context = Timber::get_context();
$context["posts"] = new \Timber\PostQuery();
// print the results
dump($context['posts']);
更新: sql 结果输出这个查询:
SELECT 6288gjvs_posts.* FROM 6288gjvs_posts
WHERE 1=1
AND 6288gjvs_posts.post_type = 'product'
AND (6288gjvs_posts.post_status = 'publish'
OR 6288gjvs_posts.post_status = 'acf-disabled'
OR 6288gjvs_posts.post_status = 'complete'
OR 6288gjvs_posts.post_status = 'paid'
OR 6288gjvs_posts.post_status = 'confirmed'
OR 6288gjvs_posts.post_status = 'unpaid'
OR 6288gjvs_posts.post_status = 'pending-confirmation'
OR 6288gjvs_posts.post_status = 'cancelled'
OR 6288gjvs_posts.post_status = 'private')
GROUP BY 6288gjvs_postmeta.meta_key = 'place'
ORDER BY 6288gjvs_posts.menu_order, RAND(1348526234)
将其复制并粘贴到 phpmyadmin 上并显示错误输出: #1054 - 组声明中不知道冠军 '6288gjvs_postmeta.meta_key'
所以我离开加入帖子元:
SELECT 6288gjvs_posts.post_title, 6288gjvs_posts.ID, 6288gjvs_postmeta.post_id
FROM 6288gjvs_posts
LEFT JOIN 6288gjvs_postmeta
ON 6288gjvs_posts.ID = 6288gjvs_postmeta.post_id
AND 6288gjvs_posts.post_type = 'product'
GROUP BY 6288gjvs_postmeta.meta_key = 'place'
没有成功
更新 2
你好,所以最后我选择了两个查询,我没有找到任何解决方案,现在我必须在一页中管理两个分页:(。如果有人找到解决方案,我总是跟进这篇文章,可能会有用为他人。
感谢您的帮助
【问题讨论】:
-
你好,是的,但我不明白是什么关系?
标签: wordpress woocommerce