On WordPress' admin Comments Screen, when searching comments, I'd like the option to only return comments where the author entered something in the optional URL/website field. I am not looking for a way to search within the URL field, but rather, just a way to exclude comments with an empty URL field from my regular comment search.
I've come up with the following working solution that lets me include 'has:url' in my search query to achieve what I want:
add_filter('pre_get_comments','set_has_url_flag');
function set_has_url_flag($query){
global $pagenow;
global $onlyhasurl;
if($pagenow == 'edit-comments.php' && strpos($query->query_vars['search'],"has:url")!==FALSE){
$query->query_vars['search'] = trim(preg_replace('!\s+!', ' ', str_replace("has:url","",$query->query_vars['search'])));
$onlyhasurl = TRUE;
return $query;
}
return $query;
}
add_filter('the_comments', 'filter_comments_for_has_url_flag');
function filter_comments_for_has_url_flag($comments){
global $pagenow;
global $onlyhasurl;
if($pagenow == 'edit-comments.php' && isset($onlyhasurl) && $onlyhasurl===TRUE){
foreach($comments as $key => $value){
if(empty($value->comment_author_url)){
unset($comments[$key]);
continue;
}
}
}
return $comments;
}
The issue with my solution is that it removes comments with empty URL fields AFTER the query is performed, so the number of comments displayed on each page of the Comments Screen varies depending on how many comments were removed because they had an empty 'author_url' value.
Can someone come up with a better way to do this that filters comments before the query is made, so that paging through comments works correctly?