[英]Wordpress - Add post title to meta field query
Question: Is there a way to add the built-in s=query
into a meta field query OR
array? 问题:有没有办法将内置的
s=query
到元字段查询OR
数组中?
We want to query on both meta data and post title, for the purpose of a search form. 为了搜索表单,我们想同时查询元数据和帖子标题。 Currently, adding the
s=query
prevents the meta_query
from functioning (which it does so perfectly without). 当前,添加
s=query
阻止meta_query
正常运行(如果没有,它会非常完美)。
PHP 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);
You could try something like this, in order to keep the original meta query structure like shown in 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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.