【问题标题】:WordPress exact search queryWordPress精确搜索查询
【发布时间】:2020-02-24 09:43:58
【问题描述】:

我需要搜索确切的标题。但我的代码有时会获取类似的标题,就像

I love him, I love, I love my country,

我只需要搜索第二个标题“我爱”。我怎样才能做到这一点?

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

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    你可以在你的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' => '!=',
    );
    

    【讨论】:

      猜你喜欢
      • 2013-10-07
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2017-06-30
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多