admin管理员组文章数量:1421572
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;
}
本文标签: wp querySearch with WPQuerybut ignore href URLs in anchor tags
版权声明:本文标题:wp query - Search with WP_Query, but ignore href URLs in anchor tags? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745347343a2654551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论