我正在嘗試為我的用戶創建自定義過濾器。這是我正在嘗試做的但不起作用的事情:
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'relation' => 'OR',
'options' => array()
);
foreach ($myurlfilter as $multifilter) {
$args['options'][] = array (
'key' => 'product_title',
'value' => "$multifilter",
'compare' => 'LIKE',
);
}
我沒有得到任何 php 錯誤,我不知道我做錯了什么。我確實根據這個答案制作了我的代碼:Foreach loop inside array,如果你在下面的答案中檢查作者的最后一條評論,它會說“代碼不起作用”。
從我看到的輸出中,我相信 foreach 代碼中的選項沒有與上面的代碼正確“連接”。
如果這有助于我試圖通過 foreach 實作的目標是這樣的,那么它非常有效:
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'product_title',
'value' => 'Leonardo',
'compare' => 'LIKE',
),
array(
'key' => 'product_title',
'value' => 'Einstein',
'compare' => 'LIKE',
)
)
);
uj5u.com熱心網友回復:
嘗試使用開頭的“或”構建您的元查詢
$myMeta = array( 'relation' => 'OR' );
foreach ($myurlfilter as $multifilter) {
$myMeta[] = array (
'key' => 'product_title',
'value' => $multifilter,
'compare' => 'LIKE',
);
}
$args['meta_query'][] = $myMeta;
我也相信你需要放棄“選項”并添加到“meta_query”
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array() // change this, remove the relation
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406554.html
標籤:
