0

I spent nearly 24 hours trying to find a solution for this issue. I'm trying to search for posts using WP_Query but it never gives a result if I don't search by the exact post title which is so unlogical to me!; I've tried all possible and logical WP_Query's arguments but still not working...

For example, I've published post, titled with (Hello and welcome!), If I search for the exact title yes I will get it, but if I change Welcome to delcome it doesn't work !! I tried to use spaces and pluses and all search cases but didn't work too ...

you can see in the below code, that I've searched by category ID which should make it closer but that didn't work too! ...

Here's my code:

$term = term_exists( 'Uncategorized', 'category' );

if ( $term !== 0 && $term !== null ) {
   
//echo $term['term_id'];
    
$args = array(
     's' => 'Hello+and+delcome!',
     'cat' => $term['term_id'],
     'posts_per_page' => 1,
     'orderby' => 'title',
     'compare' => 'LIKE',
     'exact' => false
);
    

$the_query = new WP_Query( $args );


while ( $the_query->have_posts() ) : $the_query->the_post();
    the_title();
endwhile;

wp_reset_postdata();
    
}
Alli2021
  • 25
  • 4
  • [Check your collation](https://stackoverflow.com/a/35029976/231316) to make sure things are case insensitive on the MySQL side of things. – Chris Haas Apr 03 '21 at 23:05

1 Answers1

1

There isn't anything illogical about it. You're trying to match two strings that are different and you're expecting them to evaluate to true while they are different. If you wrote an SQL query that mimics your method parameters I don't believe you'll get any results (I haven't tested but I'm pretty sure your result set would be zero).

I did look at the WP_Query page and didn't see any mention of the exact parameter.

That being said. I'm not too experienced in setting up intelligent searches. However; thinking this over in my head I would start by doing the following.

  • Run the query you have currently
  • If nothing is returned break the search string up by spaces and place each word into array.
  • Check each word for spelling using something like pspell
  • Build array of different spelling variations using the suggested words.
  • Run the array of different spelling variations in WP_Query. Put all your results into a variable.

It's probably not perfect, but might get you on a track for your solution.

InvictusMKS
  • 401
  • 3
  • 8