如果字段为空则显示所有帖子

时间:2017-09-13 08:58:49

标签: php wordpress themes

// Start the Query
$v_args = array(
    'post_type' => 'listing',
    'meta_query' => array(
        array(
            'key' => 'wpcf-services',
            'value' => $services,
            'compare' => 'LIKE',
        ),
    ),
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'listing-city',
            'field' => 'name',
            'terms' => $location,
            'compare' => 'LIKE',
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'listing-category',
            'field' => 'name',
            'terms' => $listing_category,
            'compare' => 'LIKE',
            'operator' => 'IN'
        ),
    ),
);

$listingsearch = new WP_Query( $v_args );

如果字段为空,我想在搜索结果中显示所有帖子。如果tax_querymeta_query为空,请在搜索结果中显示所有帖子。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

<?  if(isset($services)&&$services!=""){
            $meta_query[] = array(
                'key'     => 'wpcf-services',
                'value'   => $services,
                'compare' => 'LIKE',
                );
        }


         $tax_query = array('relation'=>'AND');

          if(isset($location)&&$location!=""){
             $tax_query[] = array(
                        'taxonomy' => 'listing-city',
                        'field' => 'name',
                        'terms' => $location,
                        'compare' => 'LIKE',
                        'operator' => 'IN'
                );
            }  


        if(isset($listing_category)&&$listing_category!=""){
             $tax_query[] = array(
                        'taxonomy' => 'listing-category',
                        'field' => 'name',
                        'terms' => $listing_category,
                        'compare' => 'LIKE',
                        'operator' => 'IN'
                );
            }     


        $v_args = array(
            'post_type'  =>  'listing',
            'meta_query' => $meta_query,
            'tax_query' =>$tax_query,

        );

 $listingsearch = new WP_Query( $v_args );?>

这对你有用。 它将检查空,如果找到,它将获取所有产品。否则它会相应地获取。如果有更多建议,请告诉我。

由于

答案 1 :(得分:0)

我猜你可能是这个意思?:

$v_args = array(
'post_type'     =>  'listing',
'meta_query' => array(
            array(
            'key'     => 'wpcf-services',
            'value'   => empty($services)?'%':$services,
            'compare' => 'LIKE',
            ),
            ),
'tax_query' =>array(
                'relation' => 'AND',
        array(
                'taxonomy' => 'listing-city',
                'field' => 'name',
                'terms' => empty($location)?'%':$location,
                'compare' => 'LIKE',
                'operator' => 'IN'
        ),
        array(
                'taxonomy' => 'listing-category',
                'field' => 'name',
                'terms' => empty($listing_category)?'%':$listing_category,
                'compare' => 'LIKE',
                'operator' => 'IN'
        ),
        ),

);