WordPress – 优化同时处理3个元数据键的元查询

huangapple go评论57阅读模式
英文:

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

&lt;?php
$price_from = &#39;20&#39;;
$price_to = &#39;700&#39;;

$meta_query_price[] = array(
    &#39;relation&#39; =&gt; &#39;OR&#39;,
    array(
        &#39;relation&#39; =&gt; &#39;AND&#39;,
        array(
            &#39;key&#39; =&gt; &#39;price&#39;,
            &#39;value&#39;    =&gt;  $price_from,
            &#39;type&#39;     =&gt; &#39;NUMERIC&#39;,
            &#39;compare&#39; =&gt; &#39;&lt;=&#39;
        ),
        array(
            &#39;key&#39; =&gt; &#39;price_to&#39;,
            &#39;value&#39;    =&gt;  $price_to,
            &#39;type&#39;     =&gt; &#39;NUMERIC&#39;,
            &#39;compare&#39;  =&gt; &#39;&gt;=&#39;
        ),
    ),
    array(
        &#39;key&#39; =&gt; &#39;price_type&#39;,
        &#39;value&#39;    =&gt;  &#39;POA&#39;,
        &#39;type&#39;    =&gt;  &#39;value&#39;,
        &#39;compare&#39;  =&gt; &#39;LIKE&#39;
    ),
);

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.

&lt;?php
$price_from = &#39;20&#39;;
$price_to = &#39;700&#39;;

$meta_query_price[] = array(
    &#39;relation&#39; =&gt; &#39;OR&#39;,
    array(
        &#39;key&#39; =&gt; &#39;price_type&#39;,
        &#39;value&#39;    =&gt;  &#39;POA&#39;,
        &#39;type&#39;    =&gt;  &#39;value&#39;,
        &#39;compare&#39;  =&gt; &#39;LIKE&#39;
    ),
    array(
        &#39;relation&#39; =&gt; &#39;AND&#39;,
        array(
            &#39;key&#39; =&gt; &#39;price&#39;,
            &#39;value&#39;    =&gt;  $price_from,
            &#39;type&#39;     =&gt; &#39;NUMERIC&#39;,
            &#39;compare&#39; =&gt; &#39;&lt;=&#39;
        ),
        array(
            &#39;key&#39; =&gt; &#39;price_to&#39;,
            &#39;value&#39;    =&gt;  $price_to,
            &#39;type&#39;     =&gt; &#39;NUMERIC&#39;,
            &#39;compare&#39;  =&gt; &#39;&gt;=&#39;
        ),
    ),
);

huangapple
  • 本文由 发表于 2023年5月17日 21:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定