6

I have a a CPT set up with a custom taxonomy attached to it. I recently noticed that if I search for 0 (zero), in the back-end as well as the front-end, it gives me results. None of these resultant posts have 0 anywhere in the title as well as the content. I have not modified the loop in anyway, it is just the plain search template. This makes me wonder about 2 things:

  1. What does WP search through by default?
  2. Is there a way to stop it from searching for 0?

Any ideas?

Update:

From the comments below, I am updating my requirement:

Is there a way to search for the posts which contain 0 in either the title or the content, when 0 is entered as the search key?

  • 3
    I've just tested in a blank theme with dummy data, search with 0 (zero) fired an empty search - that means all: SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10 – Mayeenul Islam Sep 15 '15 at 6:56
  • So it will give me all the content? Any way to stop it? – Rutwick Gangurde Sep 15 '15 at 7:29
  • 3
    The fact that wp-includes/query.php contains condition: if(!empty($q['s'])){$search = $this->parse_search($q);}. When your ?s=0 this conditions not true and stop using a parse_search. A parse_search function generates LIKE string in addition to a general query. Considering above, when ?s=0-> you does not have LIKE part of the query provided by parse_search and, as @Mayeenul Islam mentioned - "all" returned. – Ignat B. Sep 15 '15 at 7:33
  • Thanks both of you. Please add an answer and I will accept it. I think I'll provide some other way to avoid this from happening. – Rutwick Gangurde Sep 15 '15 at 7:39
  • 2
    I think as long as we are not getting an answer regarding Is there a way to stop it from searching for 0? we should look for a perfect answer. – Mayeenul Islam Sep 15 '15 at 7:42
5

Considering @Mayeenul Islam comment:

I've just tested in a blank theme with dummy data, search with 0 (zero) fired an empty search - that means all:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_date DESC LIMIT 0, 10

and the fact that wp-includes/query.php contains

if ( ! empty( $q['s'] ) ) {
    $search = $this->parse_search( $q );
}

, we can see that !empty($q['s']), where $q['s'] == 0 (or "0") will return FALSE. This means that search query wont have LIKE part, when $q['s'] == 0 and it return "all" posts since nothing restrict query with specific conditions, that should be inside LIKE part.

This is confirmed by empty() function documentation:

The following things are considered to be empty:

...

  • 0 (0 as an integer)
  • "0" (0 as a string)

...

Due to that we're not able to filter/update, "0" in order to pass if-empty conditional logic. What I suggest is a query updates in case if users are going to search for "0".

Here is a quickfix how to allow users to search for "0".

add_filter("posts_where", "search_hotfix", 10, 1);
function search_hotfix($where_clause){

    if(get_query_var('s') == 0){
      $where_clause .=  " AND (((wp_posts.post_title LIKE '%0%') OR (wp_posts.post_content LIKE '%0%'))) ";
    }

    return $where_clause; 
}

UPDATE:

And here is a quickfix how to stop WP search for "0". This code will force WP_Query to provide no results.

add_action('pre_get_posts', 'search_hotfix_reset_q');
function search_hotfix_reset_q($query){
    if ( get_query_var('s') == 0) {
        $query->query_vars['post__in'] = array(0);
    }
}
  • 2
    IIRC, if you pass an empty array to post__in, WP_Query will return all posts regardless. – Pieter Goosen Sep 15 '15 at 12:38
  • Recently tested on WP 4.3 twentyfifteen the array(0) solution - works. array() - yep, all posts. The fact that there are no post with ID=0 in WP installantion usually ;) – Ignat B. Sep 15 '15 at 12:40
  • I have updated my question details. How about only searching the posts which have a 0 in either the title or the content when 0 is the search key? – Rutwick Gangurde Sep 15 '15 at 14:19
  • 1
    @Rutwick Gangurde the code with add_filter("posts_where", "search_hotfix", 10, 1); is a hotfix that allow users to search for "0" in title and content – Ignat B. Sep 15 '15 at 14:42
  • 2
    @RutwickGangurde note that this approach can affect other queries too on the page, so you need to restrict it further, e.g. to the main query. – birgire Sep 15 '15 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.