Wordpress WP_Query搜索标题和内容?

时间:2014-06-10 15:50:07

标签: php wordpress-plugin wordpress

" s" WP_Query()中的参数只搜索标题? 如何仅搜索内容? 如何让它搜索标题和&内容?

2 个答案:

答案 0 :(得分:1)

啊,啊,我找到了解决方案。

         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");
         }

答案 1 :(得分:0)

您无法按帖子内容过滤查询。你必须编写一个循环遍历所有帖子寻找该字段的函数。