问题:有没有办法将内置s=query
添加到元字段查询OR
数组中?
我们想要查询元数据和帖子标题,以便搜索表单。目前,添加s=query
会阻止meta_query
正常运行(没有这样做完全没有)。
PHP
// Our search request
$queryString = sanitize_text_field($_REQUEST['query']);
// List of ACF fields to loop through
$customFields = array('title', 'subtitle', 'page_content');
// Setup basic "OR" meta_query for ACF fields
$customFieldsMetaQuery = array(
'relation' => 'OR'
);
// Insert given ACF fields into meta_query
foreach ($customFields as $field) {
$customFieldsMetaQuery[] = array(
'key' => $field,
'value' => $queryString,
'compare' => 'LIKE',
);
}
// Query Arguments
$args = array(
'post_type' => array('page'),
's' => $queryString, // This line clashes with 'meta_query'
'meta_query' => $customFieldsMetaQuery, // This line clashes with 's'
);
// Construct Query
$searchQuery = new WP_Query($args);
答案 0 :(得分:0)
您可以尝试这样的操作,以保持原始元查询结构,如here所示。
// Insert given ACF fields into meta_query
foreach ($customFields as $field) {
$customFieldsMetaQuery[] = array(
'key' => $field,
'value' => $queryString,
'compare' => 'LIKE',
);
}
// Setup basic "OR" meta_query for ACF fields
$customFields = array(
'relation' => 'OR',
$customFieldsMetaQuery
);