自定义 wp_query 其中 meta_key 可变或不需要

custom wp_query where meta_key is variable or not needed

为了使自定义 post 类型可过滤和排序,我创建了一个自定义查询,该查询基于 url 中的变量,由 $_GET 访问。 一种特定情况打破了它。这是一个(未)指定的元密钥。在排序的情况下,在自定义字段上排序时需要元键。在其他情况下,可以忽略元键。但是,当我将变量设置为空时,查询不会产生任何 posts。如何处理空meta_key?

到目前为止,我尝试将变量设置为空($variable =''); 我将变量设置为空;我用过unset.

if (isset($_GET["key"]) || isset($_GET["orderby"])){
if (isset($_GET["key"])) {$key = $_GET["key"];}
else {$key = '';}
if (isset($_GET["value"])) {$value = $_GET["value"]; echo $value;}
else {$value = '';}
if (isset($_GET["orderby"])) {$orderby = $_GET["orderby"];}
if ($orderby='meta_value'){$meta_key='averagerating';}
else {$orderby='';$meta_key='';}
if (isset($_GET["order"])) {$order = $_GET["order"];}
else {$order='';}
$cat = get_queried_object();
if (!empty($cat) && !is_post_type_archive('bedrijf')){
$category = $cat->name;
}

if (is_post_type_archive('bedrijf')){
$category = '';
$terms = get_terms( 'bedrijfs-category' ); 
    // convert array of term objects to array of term IDs
$category = wp_list_pluck( $terms, 'name' );    
}
global $post;
                $the_query ='';
                $args = array(
                    'post_type' => 'bedrijf',
                    'posts_per_page' => 10,
                    'meta_key' => ''.$meta_key.'',
                    'orderby'   => ''.$orderby.'',
                    'order' => ''.$order.'',
                    'tax_query' => array(
                                        array(
                                            'taxonomy' => 'bedrijfs-category',
                                            'field'    => 'name',
                                            'terms'    => $category
                                            )
                                        ),
                    'meta_query' => array(
                                        array(
                                            'key'=> ''.$key.'', 
                                            'value' => ''.$value.'',
                                            'compare' => 'LIKE'
                        )   
                    )
                );
 $the_query = new WP_Query($args); }

我想要的是在 meta_key => ''.$meta_key.'' 中插入空变量的解决方案,因此循环会跳过 meta_key 部分。

从 $args 中定义 meta_query 或 tax_query 和(示例):

if($_GET['some_name'] !== ''){
  $meta_query = array(
    array(
         'taxonomy' => 'bedrijfs-category',
         'field'    => 'name',
         'terms'    => $category
    )
  );
  //define args with some_one para exists
  $args = array(
        'post_type' => 'bedrijf',
        'posts_per_page' => 10,
        'meta_key' => ''.$meta_key.'',
        'orderby'   => ''.$orderby.'',
        'order' => ''.$order.'',
        'meta_query' => $meta_query
  );

}else{

    //define args with some_one para not exists
    $args = array(
        'post_type' => 'bedrijf',
        'posts_per_page' => 10,
        'meta_key' => ''.$meta_key.'',
        'orderby'   => ''.$orderby.'',
        'order' => ''.$order.''
  );

 }

tax_query是这样的