我需要修改或创建一个WP\\U查询,该查询将在帖子标题或自定义字段(称为“my\\u字段”)中搜索搜索词。
我已经阅读并尝试了几个小时,但我马上又回到了这段代码(下面),唉,它只在“my\\u字段”中搜索,不考虑post\\u标题。
function my_pre_get_posts_2( $query ) {
if ( is_admin() && $query->is_main_query() && $query->query[\'post_type\'] === \'post\' && isset($query->query[\'s\']) ) {
$search_word = $query->query[\'s\'];
$args = array(
//\'s\' => $search_word, //If I include this line, the WP query seems to AND post_title and my_field. If I comment out this line, the WP query only searches in my_field
\'post_type\' => \'post\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'my_field\',
\'value\' => $search_word,
\'compare\' => \'IN\'
),
array(
\'key\' => \'post_title\',
\'value\' => $search_word,
\'compare\' => \'IN\',
)
)
);
//$query = new WP_Query( $args ); //Need to modify the existing WP_Query
$query->init();
$query->parse_query($args);
}
}
add_action( \'pre_get_posts\', \'my_pre_get_posts_2\' );
我之所以需要这样做,是因为我需要修改“所有帖子”(admin)页面中“搜索帖子”按钮的行为,以便管理员用户搜索的内容都将返回具有匹配帖子标题或my\\u字段值的帖子。