简体   繁体   English

将过滤器添加到 wordpress 查询

[英]Add filter to wordpress query

My issue is as follows;我的问题如下; I have fields with no value showing on my page - I want them to be hidden, until they have a value.我的页面上没有显示任何值的字段 - 我希望它们被隐藏,直到它们具有值。 In my code I have a meta_value_num as a sorting option per today.在我的代码中,我每天都有一个meta_value_num作为排序选项。 But it still shows values that are 0(zero) and blanks.但它仍然显示 0(零)和空白的值。

Please tell me how I can filter these so that the empty ones are not shown.请告诉我如何过滤这些以便不显示空的。 Eg if I can sort everything with a value = 100 and above, that would be good enough.例如,如果我可以对值 = 100 及以上的所有内容进行排序,那就足够了。

This is how the code looks per today:这就是今天代码的样子:

$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array( 
    'post_type' => 'clinic',
    'meta_key' => $treatment,
    'orderby' => 'meta_value_num',
    'order' => 'DSC',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'location',
            'field'    => 'ID',
            'terms'    => $tax->term_id
             )
        ),
));

I managed to fix the filter issue.我设法解决了过滤器问题。

By adding this code it now filters out everything below 20 and shows all above.通过添加此代码,它现在会过滤掉 20 以下的所有内容并显示所有内容。 Big shout out to @Cbroe for pointing me in the right direction!向@Cbroe 大喊大叫,为我指明了正确的方向!

'meta_value' => '20', 'meta_compare' => '>=', 'meta_value' => '20', 'meta_compare' => '>=',

Code now looks like this;代码现在看起来像这样;

$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array( 
    'post_type' => 'clinic',
    'meta_key' => $treatment,
    'meta_value'   => '20',
    'meta_compare' => '>=',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'location',
            'field'    => 'ID',
            'terms'    => $tax->term_id
             )
        ),
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM