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).

1 Answer 1


Please 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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.