I am making a wildcard search query on the Wordpress database for "Post Title".
My results would vary:
I would like to limit my wildcard results to:
Any idea or algorithm I can achieve this?
Code
$query = "SELECT ID, `post_title` FROM `wp_posts` WHERE `post_title` LIKE '%".$titleRequest."%'";
$results = $wpdb->get_results($query);
Modify the query like this:
$query = "SELECT ID, `post_title` FROM `wp_posts` WHERE `post_title` LIKE '".$titleRequest."%'";
$results = $wpdb->get_results($query);
The initial %
means "match anything here" - but you do not want to do that, you want to match only things that start with the title requested. The second %
is ok - you do want to match things after Post Title, after all.
在您的SQL
查询中的WHERE
子句中尝试以下操作:
WHERE FieldName LIKE 'Post Title%';
You will need to implement fuzzy logic in your search. But its a very complex algorithm, fortunately there are already some plugins that have implimented this algorithm like " Relevanssi ". Once installed it will override your current search mechanism and add its own, then you can modify it accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.