[英]Wordpress WP_Query search both title and content?
WP_Query()中的“ s”参数仅搜索标题? 如何使其仅搜索内容? 如何使其同时搜索标题和内容?
啊,我找到了解决方案。
function where_title_only($where_clause) {
return ($where_clause." AND `post_title` LIKE '%".trim($_POST['k'])."%' ");
}
function where_desc_only($where_clause) {
return ($where_clause." AND `post_content` LIKE '%".trim($_POST['k'])."%' ");
}
function where_both($where_clause) {
return ($where_clause." AND (`post_title` LIKE '%".trim($_POST['k'])."%' "." OR `post_content` LIKE '%".trim($_POST['k'])."%' ) ");
}
if (trim($_POST['search_type']) == "title") {
add_filter("posts_where","where_title_only");
}if (trim($_POST['search_type']) == "desc") {
add_filter("posts_where","where_desc_only");
}else{
add_filter("posts_where","where_both");
}
您不能按帖子内容过滤查询。 您将必须编写一个遍历所有查找字段的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.