Php WordPress精确搜索查询
我需要搜索确切的标题。但我的代码有时会得到类似的标题Php WordPress精确搜索查询,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我需要搜索确切的标题。但我的代码有时会得到类似的标题 我爱他, 我爱, 我爱我的国家, 我只需搜索第二个标题“我爱””。我怎样才能做到这一点 $title= "I love"; global $post; $args = array( 'post_type' => 'course', "s" => $title, 'post_status' => 'publish', 'posts_per_pag
我爱他,
我爱,
我爱我的国家,
我只需搜索第二个标题“我爱””。我怎样才能做到这一点
$title= "I love";
global $post;
$args = array(
'post_type' => 'course',
"s" => $title,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'vibe_product',
'meta_value' => ' ',
'meta_compare' => '!=',
);
您可以在functions.php中传递下面的代码
add_filter( 'posts_where', 'custom_posts_where', 10, 2 );
function custom_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $specific_title = $wp_query->get( 'specific_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title = \'' . esc_sql( $wpdb->esc_like( $specific_title ) ) . '\'';
}
return $where;
}
然后使用get_posts或wp查询功能,如下所示:
$title= "I love";
global $post;
$args = array(
'post_type' => 'course',
"specific_title" => $title,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'vibe_product',
'meta_value' => ' ',
'meta_compare' => '!=',
);