[英]Custom field query multiple post types - Wordpress
我試圖查詢數據的兩個自定義帖子類型,以下不起作用。 我嘗試了幾個版本......但沒有運氣。
$args = array(
'post_type' => array('custom1', 'custom2'),
'meta_query' => array(
'relation' => 'AND',
//the first two keys are custom fields found in custom1 custom posts
array(
'key' => 'firstKey',
'value' => 3
),
array(
'key' => 'secondKey',
'value' => cool
),
//the third key can be found in custom2 custom posts
array(
'key' => 'thirdKey',
'value' => 3
)
)
);
根據您的意見:如果要對不同類型的用戶使用自定義查詢,則可能需要動態創建查詢。
例如,如果“酷”類型的用戶獲得特殊待遇,那么基本想法將是:
$args = array( 'post_type' => 'custom' );
if (user is 'cool') {
$args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'secondKey',
'value' => 'cool'
),
array(
'key' => 'thirdKey',
'value' => 3
)
};
} else {
$args['meta-key'] = 'thirdKey';
$args['meta_value'] = 3
}
這只是像WP_Query($ args)那樣傳遞它,如果它是靜態的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.