user1187968
user1187968

Reputation: 8026

Wordpress WP_Query search both title and content?

The "s" parameter in WP_Query() only search the title? How do I make it search content only? How do I make it search both title & content?

Upvotes: 0

Views: 2242

Answers (2)

user1187968
user1187968

Reputation: 8026

Ahhhh, I found a solution.

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

Upvotes: 1

Jon Lachonis
Jon Lachonis

Reputation: 931

You cannot filter Query by post content. You would have to write a function that loops through all posts looking for the field.

Upvotes: 0

Related Questions