最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Search with WP_Query, but ignore href URLs in anchor tags?

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}
发布评论

评论列表(0)

  1. 暂无评论