Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress精确搜索查询_Php_Wordpress_Custom Post Type - Fatal编程技术网

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' => '!=',
);