WP_Query min and max values

时间:2018-03-03 作者:John McGuinnes

我正在构建一个houses类,我想用查询过滤这些房屋。但没有得到任何结果:

示例房屋:
最低价格:10
最高价格(不需要字段):空

我设置了价格过滤器Min price 6 - Max price 15我使用此查询获取结果,但它不起作用。

new \\WP_Query(
    [
        \'post_type\'  => \'properties\',
        \'meta_query\' => [
            \'relation\'      => \'AND\',
            \'min_price\' => [
                \'relation\' => \'AND\',
                [
                    \'key\'     => \'min_price\',
                    \'value\'   => 6,
                    \'compare\' => \'>=\',
                    \'type\' => \'NUMERIC\'
                ],
            ],
            \'max_price\' => [
                \'relation\' => \'AND\',
                [
                    \'key\'     => \'max_price\',
                    \'value\'   => 11,
                    \'compare\' => \'<=\',
                    \'type\' => \'NUMERIC\'
                ],
                [
                    \'key\'     => \'max_price\',
                    \'value\'   => \'\',
                    \'compare\' => \'!=\',
                ],
            ],
        ],
    ]
);
我还想知道如何使它与所需的最小值和可选的最大值一起工作。我对卧室、浴室等也有同样的问题。

提前感谢!

这是我用于测试目的的简单示例,但它不起作用。最小价格不是问题,但当我添加最大价格时,它不起作用。

\'relation\' => \'AND\',
[
    \'key\' => \'min_price\',
    \'value\' => 6,
    \'type\' => \'NUMERIC\',
    \'compare\' => \'>=\'
],
[
    \'key\' => \'max_price\',
    \'value\' => 13,
    \'type\' => \'NUMERIC\',
    \'compare\' => \'<=\'
]

SQL QUERY

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  ( 
    ( wp_postmeta.meta_key = \'min_price\' AND CAST(wp_postmeta.meta_value AS SIGNED) >= \'6\' ) 
    AND 
    ( mt1.meta_key = \'max_price\' AND CAST(mt1.meta_value AS SIGNED) <= \'13\' )
  )
) AND wp_posts.post_type = \'properties\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 999

Update CAUSES

\'relation\' => \'OR\',
[
    \'key\' => \'min_price\',
    \'value\' => 6,
    \'type\' => \'NUMERIC\',
    \'compare\' => \'>=\'
],
[
    \'key\' => \'max_price\',
    \'type\' => \'NUMERIC\',
    \'value\' => [6, 13],
    \'compare\' => \'BETWEEN\'
]

1 个回复
SO网友:Felipe Elia

取决于脚本的录制方式max_price, 它实际上不是上的meta\\u键/meta\\u值postmeta 桌子

你有没有试过把最高价格条款改成这样?

[\'max_price\'] => [
    \'relation\' => \'OR\',
    [
        \'key\' => \'max_price\',
        \'value\' => 11,
        \'compare\' => \'<=\',
    ],
    [
        \'key\' => \'max_price\',
        \'compare\' => \'NOT EXISTS\',
    ],
],

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post