在WordPress的管理评论屏幕上,当搜索评论时,我想选择只返回作者在可选URL/网站字段中输入内容的评论。我不是在寻找一种在URL字段中搜索的方法,而是一种从我的常规评论搜索中排除带有空URL字段的评论的方法。
我提出了以下可行的解决方案,可以在搜索查询中包含“has:url”,以实现我的目标:
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;
}
我的解决方案的问题是,它删除了带有空URL字段的注释AFTER 执行查询时,注释屏幕的每页上显示的注释数会因删除了多少注释而有所不同,因为它们的“author\\u url”值为空。有没有人能想出一种更好的方法来做到这一点,即在进行查询之前过滤注释,以便正确地分页注释?