我如何改善这个查询在WordPress,如果嵌套的meta_query是不OK


How can I improve this query in WordPress, If nested meta_query is not OK?

我有这个问题,过滤价格条件的房间。

用户将搜索价格低于500且具有电视冷热水淋浴的房间。

Legend:    
rw_prop_price = amount 
rw_prop_rooms1 = checkbox with/without TV
rw_prop_rooms2 = checkbox with/without Tub
rw_prop_rooms3 = checkbox with/without cold/hot shower

这是我的代码,但它不工作,因为它不可能使用嵌套的meta_query。

$args = array(
     'post_type' => 'rw_property',
     'posts_per_page' => 10,
     'paged' => get_query_var('paged'),
     'relation'=> 'AND', 
     'meta_query' => array(
         'relation' => 'AND', 
            array(
             'key' =>'rw_prop_price',
             'value' => 500, 
             'type' => 'numeric',
             'compare' => '<'  
            ),                                                                      
            array(
               'relation' => 'OR',
                  array(
                    'key' =>'rw_prop_rooms1',
                    'value' => 'yes', 
                    'compare' => '='  
                  ),
                  array(
                    'key' =>'rw_prop_rooms2',
                    'value' => 'no', 
                    'compare' => '='  
                  ),
                  array(
                    'key' =>'rw_prop_rooms3',
                    'value' => 'yes', 
                    'compare' => '='  
                  )
            )                                                                  
        ),                                       
     'post_status' => 'publish'
     );
$wp_query =  new WP_Query( $args );

谁能帮我怎么能做到这一点,我想尽可能多地使用WordPress的WP_Query。但我愿意重组我的数据,我的意思是如果你有一个解决方案,比如合并3个房间,只是序列化等只要它能帮助我,非常感谢。

如果您想使用WP_Query,那么我认为您必须通过一个自定义值进行过滤,将其全部保存到数组中,然后过滤掉不匹配搜索条件的那些。确保查询的元值将使您的平均点击率最小(以减少PHP的负载)。这可能是一个好主意,也可能不是,这取决于你要获取多少数据,但这里有一个例子:

查询

$args = array(
     'post_type' => 'rw_property',
     'posts_per_page' => -1,
     'meta_query' => array(
         'relation' => 'AND', 
            array(
             'key' =>'rw_prop_price',
             'value' => 500, 
             'type' => 'numeric',
             'compare' => '<'  
            )
     ),                                    
     'post_status' => 'publish'
);
$wp_query =  new WP_Query( $args );

.

<

过滤器/h3>
$results = array();
# Set these values based on user input (search filter settings)
$tv = 'yes';
$hot_tub = 'no';
$shower = 'yes';
# Iterate through all the posts, and select only the ones that match our criteria 
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    
    if( 
        get_post_meta( get_the_ID(), 'rw_prop_rooms1', true ) == $tv &&
        get_post_meta( get_the_ID(), 'rw_prop_rooms2', true ) == $hot_tub &&
        get_post_meta( get_the_ID(), 'rw_prop_rooms3', true ) == $shower
    )
    
    # Store the post object if criteria matches
    $results[] = $wp_query;
    
endwhile;

.

表示

foreach( $results as $post ) 
    echo $post->post_title;

.

这是完全未经测试的,所以不能保证代码将运行。此外,它还会搞砸您的分页,我猜这在您的情况下可能是个问题(您必须构建自己的分页)。如果你想在WordPress中比较很多自定义值,我想这就是你需要做的。如果有更简单的方法,我也有兴趣学习。