如何使用SQL查询来搜索帖子标题以查找包含搜索词wordpress的帖子

时间:2019-12-06 18:34:55

标签: sql wordpress

由于速度原因,我有一个Wordpress网站,我需要使用SQL查询。

我遇到的问题是,当用户搜索SQL查询时,仅显示完全匹配项,而没有包含搜索词的帖子。

如何使用包含搜索词但不完全匹配的SQL查询帖子?

这里是我的SQL查询

 $sql = $wpdb->prepare( "
    SELECT DISTINCT
        p.ID,
        p.post_date,
        p.post_title,
        p.guid,
        featured_img.meta_value as featured_img,
        price.meta_value as price,
        map_lat.meta_value as locLat,
        map_lng.meta_value as locLong,
        ( %d * acos(
        cos( radians( %s ) )
        * cos( radians( map_lat.meta_value ) )
        * cos( radians( map_lng.meta_value ) - radians( %s ) )
        + sin( radians( %s ) )
        * sin( radians( map_lat.meta_value ) )
        ) )
        AS distance
    FROM $wpdb->posts p
    INNER JOIN $wpdb->term_relationships rel ON p.ID = rel.object_id
    INNER JOIN $wpdb->term_taxonomy tax ON rel.term_taxonomy_id = tax.term_taxonomy_id
    INNER JOIN $wpdb->terms t ON tax.term_id = t.term_id
    INNER JOIN $wpdb->postmeta map_lat ON p.ID = map_lat.post_id
    INNER JOIN $wpdb->postmeta map_lng ON p.ID = map_lng.post_id
    INNER JOIN $wpdb->postmeta featured_img ON p.ID = featured_img.post_id
    INNER JOIN $wpdb->postmeta price ON p.ID = price.post_id
    WHERE 1 = 1
    AND p.post_type = 'product'
    AND p.post_status = 'publish'
    AND tax.taxonomy = '$taxonomy'
    AND t.slug = '$term_slug'
    AND featured_img.meta_key = '_thumbnail_id'
    AND price.meta_key = '_price'  
    AND map_lat.meta_key = 'prod-lat'
    AND map_lng.meta_key = 'prod-lng'
     AND p.post_title LIKE '$search'   /// this is the line that only shows exact matches not post that contain the search terms
    HAVING distance < %s
    AND price BETWEEN $min AND $max
    ORDER BY $orderby $order",
    $earth_radius,
    $lat,
    $lng,
    $lat,
    $distance,
    $taxonomy,
    $term_slug,
    $orderby,
    $min,
    $max,
    $search
);

0 个答案:

没有答案