[英]How to merge two arrays in one arguments with WordPress
我在WordPress自定義搜索功能中有兩個數組用於一個參數,如下所示
// If get community then
if(isset($_GET['community'])){
$community = $_GET['community'];
$community_count = count($community);
for ($i=0; $i < $community_count ; $i++) {
$community_array = array(
'key' => 'community',
'value' => $_GET['community'][$i],
'compare' => 'LIKE'
);
}
}
// If get features then
if(isset($_GET['ap_features'])){
$features = $_GET['ap_features'];
$features_count = count($features);
for ($i=0; $i < $features_count ; $i++) {
$features_array = array(
'key' => 'apartment_features',
'value' => $_GET['ap_features'][$i],
'compare' => 'LIKE'
);
}
}
我想將這兩個數組合並為一個搜索參數,如下所示
// Search ARGS
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => array(
'relation' => 'AND',
array (
'relation' => 'AND',
$community_array
),
array (
'relation' => 'AND',
$features_array
),
)
);
這里是$community_array
和$features_array
當我從那么只有選擇像$community_array
那么它的搜索罰款僅$community_array
但是,當點擊$features_array
與$community_array
那么他沒有搜索任何東西,我的意思是,現在只能搜索單一,不混合。 我的問題是如何使用合並這兩個數組進行搜索。
在第一個答案評論后編輯
var_dump
結果
array(4) {
[0]=> array(3) {
["key"]=> string(9) "community"
["value"]=> string(12) "eleven_polls"
["compare"]=> string(4) "LIKE"
}
[1]=> array(3) {
["key"]=> string(9) "community"
["value"]=> string(22) "activities_coordinator"
["compare"]=> string(4) "LIKE"
}
[2]=> array(3) {
["key"]=> string(18) "apartment_features"
["value"]=> string(20) "built_in_bookshelves"
["compare"]=> string(4) "LIKE"
}
[3]=> array(3) {
["key"]=> string(18) "apartment_features"
["value"]=> string(11) "cable_ready"
["compare"]=> string(4) "LIKE" }
}
for ($i=0; $i < $features_count ; $i++) {
$features_array[] = array(
'key' => 'apartment_features',
'value' => $_GET['ap_features'][$i],
'compare' => 'LIKE'
);
}
更改上面的循環代碼,以便將每個單元功能都推入數組,否則將僅填充數組中的最后一個值,因為您不是按推而是直接分配。 並嘗試下面的代碼
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => array(
'relation' => 'AND',
array_merge($community_array, $features_array),
)
);
$meta_query = array('relation' => 'AND');
if(isset($_GET['community'])){
$community = $_GET['community'];
$community_count = count($community);
for ($i=0; $i < $community_count ; $i++) {
$community_array = array(
'key' => 'community',
'value' => $_GET['community'][$i],
'compare' => 'LIKE'
);
}
if(count($community_array) > 1):
$community_array['relation'] = 'AND';
endif;
$meta_query[] = $community_array;
}
// If get features then
if(isset($_GET['ap_features'])){
$features = $_GET['ap_features'];
$features_count = count($features);
for ($i=0; $i < $features_count ; $i++) {
$features_array = array(
'key' => 'apartment_features',
'value' => $_GET['ap_features'][$i],
'compare' => 'LIKE'
);
}
if(count($features_array) > 1):
$features_array['relation'] = 'AND';
endif;
$meta_query[] = $features_array;
}
// Search ARGS
$args = array(
'numberposts' => -1,
'post_type' => 'apartment',
'meta_query' => $meta_query
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.