我正在寻找一种只显示包含所请求的所有自定义字段的POST结果的方法。不是一个或另一个。我一次只能使用一个数组获得结果。任何帮助都将不胜感激。
$documents = array(
\'post_type\' => \'documents\',
\'meta_query\' => array(
array(
\'key\' => \'document-type\',
\'value\' => \'manual\',
),
array(
\'key\' => \'document-status\',
\'value\' => \'current\',
)
)
);
query_posts( $documents );
get_template_part( \'loop\', \'documents\' );
wp_reset_query();
我还尝试了一个SQL查询,但没有成功。
$documents = "
SELECT wposts.*
FROM $wpdb->posts wposts
JOIN $wpdb->postmeta document-status ON
( wposts.ID = document-status.post_id AND
document-status.meta_key = \'document-status\' )
JOIN $wpdb->postmeta document-type ON
( wposts.ID = document-type.post_id AND
document-type.meta_key = \'document-type\' )
WHERE document-status.meta_value = \'current\'
AND document-type.meta_value = \'manual\'
AND wposts.post_status = \'publish\'
AND wposts.post_type = \'documents\'
AND wposts.post_date < NOW()
ORDER BY wposts.post_date DESC
LIMIT 0 , 4
";
$documents = $wpdb->get_results($documents, OBJECT);
query_posts( $documents );
get_template_part( \'loop\', \'documents\' );
wp_reset_query();
最合适的回答,由SO网友:Bainternet 整理而成
meta_query
有一个relation
参数就像tax_query
因此:
$documents = array(
\'post_type\' => \'documents\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'document-type\',
\'value\' => \'manual\',
),
array(
\'key\' => \'document-status\',
\'value\' => \'current\',
)
)
);
query_posts( $documents );
get_template_part( \'loop\', \'documents\' );
wp_reset_query();