如何将两个元值传递给 WP_Query
How do I pass two meta value to WP_Query
我的 WP_Query 有问题。所以我有两个使用 ACF 创建的 select 框,一个用于国家,一个用于行业。当我 select 国家并将部门留空时,我会获得 selected 国家的所有帖子,并且对于该部门来说也是明智的。我需要做的是进一步过滤它,所以如果两者都被 selected 而不是只有一个,我会得到来自那个国家和部门的所有帖子。我的 args 下面的任何帮助表示赞赏。
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);
请尝试使用以下带有关系 AND & 比较运算符的代码。
另外请检查 WP_Query class 在 https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
'distributer_search',
'posts_per_page' => -1,
'meta_query' => 数组(
'relation' => 'AND',
大批(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
大批(
'key' => 'dis_sector',
'value' => $扇区,
'compare' => '=',
)
)
);
?>
可能对你有帮助 -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}
我的 WP_Query 有问题。所以我有两个使用 ACF 创建的 select 框,一个用于国家,一个用于行业。当我 select 国家并将部门留空时,我会获得 selected 国家的所有帖子,并且对于该部门来说也是明智的。我需要做的是进一步过滤它,所以如果两者都被 selected 而不是只有一个,我会得到来自那个国家和部门的所有帖子。我的 args 下面的任何帮助表示赞赏。
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);
请尝试使用以下带有关系 AND & 比较运算符的代码。 另外请检查 WP_Query class 在 https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
可能对你有帮助 -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}