Wordpress - 将帖子标题添加到元字段查询

时间:2015-06-12 22:49:20

标签: php wordpress advanced-custom-fields wp-query

问题:有没有办法将内置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);

1 个答案:

答案 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
);