Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've registered a custom post type called "node".

When I create a basic WP_Query to get posts of that type, it works just fine. Example:

$args = array(  
'post_status' => 'publish',
'post_type' => 'node');

$query = new WP_Query($args);       

This will get all published posts of type "node".

However, as soon as I combine this with a search, nothing is returned. Example:

$args = array(  
'post_status' => 'publish',
'post_type' => 'node',
's' => 'My search term');

$query = new WP_Query($args);

This will get nothing, although it should get several posts of type 'node' that contain 'My search term'.

As far as I can see, the post types are automatically set to "post" and "page" as soon as I include the "s" parameter in $args. If I print out a var_dump of $query, it shows the following:

Without "s":

object(WP_Query)
  public 'query' => 
    array (size=2)
      'post_status' => string 'publish' (length=7)
      'post_type' => string 'node' (length=4)
  public 'query_vars' => 
    array (size=63)
      'post_status' => string 'publish' (length=7)
      'post_type' => string 'node' (length=4)
...

With "s":

object(WP_Query)
  public 'query' => 
    array (size=2)
      'post_status' => string 'publish' (length=7)
      's' => string 'My search term' (length=14)
      'post_type' => string 'node' (length=4)
  public 'query_vars' => 
    array (size=66)
      'post_status' => string 'publish' (length=7)
      's' => string 'My search term' (length=14)
      'post_type' => 
        array (size=2)
          0 => string 'post' (length=4)
          1 => string 'page' (length=4)
...

So Wordpress seems to override the post types as soon as a search is involved.

How can I fix that?

share|improve this question
1  
I cannot confirm this. I just tested something very similar - successfully. Have you tried without any plugin and/or using a default theme? Even the search (i.e., the main search, using the search form) includes all custom post types that are not explicitly excluded from search. –  t f Feb 13 at 18:57
2  
I actually wrote a search this morning that used post_type (a cpt), post_status (publish) and s and it's working fine. You almost certainly have a filter or something in a plugin/theme as suggested by @tf. –  Andrew Bartel Feb 13 at 21:36
    
I have not been able to test this but I am about 95% sure I have don something similar and had it work. I agree with the speculation about action of filter interference. –  s_ha_dum Feb 13 at 22:02
1  
You are right, I tried it in a clean Wordpress install and it works. Seems like one of the plugins I use in my environment is hooking itself in one of the pre-query events. Thanks! –  Sebastian Feb 17 at 14:57

1 Answer 1

up vote 2 down vote accepted

You are right, I tried it in a clean Wordpress install and it works.

Seems like one of the plugins I use in my environment is hooking itself in one of the pre-query events.

Thanks!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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