英文:
WordPress - Optimize the Meta Query for 3 meta keys at a time
问题
以下是已经翻译好的部分:
post_meta
中的记录如下:
post_id | key | value |
---|---|---|
1 | price | 10 |
1 | price_to | 5000 |
2 | price_type | 'POA' |
3 | price | 12000 |
3 | price_to | 17000 |
4 | price | 1200 |
4 | price_to | 8000 |
5 | price_type | 'POA' |
代码片段
<?php
$price_from = '20';
$price_to = '700';
$meta_query_price[] = array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $price_from,
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'price_to',
'value' => $price_to,
'type' => 'NUMERIC',
'compare' => '>='
),
),
array(
'key' => 'price_type',
'value' => 'POA',
'type' => 'value',
'compare' => 'LIKE'
),
);
输出
post_id | key | value |
---|---|---|
1 | price | 10 |
1 | price_to | 5000 |
2 | price_type | 'POA' |
5 | price_type | 'POA' |
上面的代码已经完美执行,但是当我将 price_type
添加到 meta_query 中以扩展数据时,会导致速度问题。
那么,在WordPress中有没有什么技巧可以执行这种查询?
英文:
Records in post_meta
as follow:
post_id | key | value |
---|---|---|
1 | price | 10 |
1 | price_to | 5000 |
2 | price_type | 'POA' |
3 | price | 12000 |
3 | price_to | 17000 |
4 | price | 1200 |
4 | price_to | 8000 |
5 | price_type | 'POA' |
Code Snippet
<?php
$price_from = '20';
$price_to = '700';
$meta_query_price[] = array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $price_from,
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'price_to',
'value' => $price_to,
'type' => 'NUMERIC',
'compare' => '>='
),
),
array(
'key' => 'price_type',
'value' => 'POA',
'type' => 'value',
'compare' => 'LIKE'
),
);
Output
post_id | key | value |
---|---|---|
1 | price | 10 |
1 | price_to | 5000 |
2 | price_type | 'POA' |
5 | price_type | 'POA' |
Above code is executed perfectly issue is when I add price_type
to the meta_query to extend data it will cause the speed issue.
The result come perfect but time will be very high.
> So, is there any tricks to do this kind of query in WordPress?
答案1
得分: 0
结论 - 在我的情况下,我尝试了以下内容
当我将最后一个元素或放在第一位时,它完美地工作了。
$price_from = '20';
$price_to = '700';
$meta_query_price[] = array(
'relation' => 'OR',
array(
'key' => 'price_type',
'value' => 'POA',
'type' => 'value',
'compare' => 'LIKE'
),
array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $price_from,
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'price_to',
'value' => $price_to,
'type' => 'NUMERIC',
'compare' => '>='
),
),
);
英文:
Conclusion - In my case I tried below
When I changed placement of last meta or at the first place worked perfectly.
<?php
$price_from = '20';
$price_to = '700';
$meta_query_price[] = array(
'relation' => 'OR',
array(
'key' => 'price_type',
'value' => 'POA',
'type' => 'value',
'compare' => 'LIKE'
),
array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $price_from,
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'price_to',
'value' => $price_to,
'type' => 'NUMERIC',
'compare' => '>='
),
),
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论