在我的搜索结果中包含来自元数据库的内容,但经过研究,我只能找到两种解决方案:
How can I include meta box content when searching? 建议使用WP_Query
但您必须为每个键编写代码:
$aquarium_H = array(
\'key\' => \'aquarium_H\', //name of your meta field
\'value\' => $_GET["s"], // value from WordPress search bar. this is sanitized by WP
\'type\' => \'numeric\', // string/numeric/whatever
\'compare\' => \'<=\' // this can be "LIKE" or "NOT LIKE". most mySQL operators
);
$meta_query[] = $aquarium_H; // add to meta_query array
和问题
How to make search include data from wp_postmeta? 建议采用类似的方法:
function me_search_query( $query ) {
if ( $query->is_search ) {
$meta_query_args = array(
array(
\'key\' => \'your_key\',
\'value\' => $query->query_vars[\'s\'] = \'\',
\'compare\' => \'LIKE\',
),
);
$query->set(\'meta_query\', $meta_query_args);
};
}
add_filter( \'pre_get_posts\', \'me_search_query\');
问题是,如果您有大量密钥,那么代码就会膨胀。现在是否有更干净的方法来调用每个键或将所有内容都包含在4.8中?