I've got a plugin which searches for related content using WP_Query. However, I'd like it to ignore words in the URLs of hyperlinks within articles. So for example, if I have this content on a page:
Read about my article on vegetables: Click here
I don't want it to return this page if I search for the word 'carrots'. Currently, because 'carrots' is in the URL, the page is returned - even though searching for 'carrots' on the page won't show any results, because the word is in the hyperlink URL, not in the actual body text.
I found a plugin which purports to do something similar (ignoring the contents of all HTML tags in searches) but it's no longer installable (although I guess I could incorporate the code into my own plugin).
I've got a plugin which searches for related content using WP_Query. However, I'd like it to ignore words in the URLs of hyperlinks within articles. So for example, if I have this content on a page:
Read about my article on vegetables: Click here
I don't want it to return this page if I search for the word 'carrots'. Currently, because 'carrots' is in the URL, the page is returned - even though searching for 'carrots' on the page won't show any results, because the word is in the hyperlink URL, not in the actual body text.
I found a plugin which purports to do something similar (ignoring the contents of all HTML tags in searches) but it's no longer installable (although I guess I could incorporate the code into my own plugin).
Share Improve this question asked Aug 26, 2018 at 13:44 WebreaperWebreaper 1013 bronze badges1 Answer
Reset to default 0Please Use This. Solution Question:- Search with WP_Query, but ignore href URLs in anchor tags. Answer->
add_filter('posts_where', 'update_search_query' );
function update_search_query($where)
{
if( is_search() ) {
global $wpdb;
$query = get_search_query();
$query = like_escape($query);
$where .=" AND {$wpdb->posts}.post_content NOT REGEXP '\<a{1}.*.$query.*.<.a\>{1}' ";
}
return $where;
}