2

I have been having this issue, with integrating events manager among my other post types, where the other custom post types show up in the $wp_query->query_vars array, except for the event manager ones. The search works, and does grab the event post types, but for some strange reason the post_type query variable does not include "event". Why is this happening? Below is the code I am using to force the query to use the event post type:

function _search_all($query) {

if($query->is_search()) {
$query->set("post_type", array(EM_POST_TYPE_EVENT, FEATURES_ID, BRIEFS_ID, CAMPUS_NEWS_ID));
}

return $query;
}
add_action("pre_get_posts", "_search_all");

EM_POST_TYPE_EVENT = 'event', and is registered in the plugin code, and it shows up everywhere else on the site, but in the search result's query variables. The other 3 post types are custom as well. The 'event' post type is set to exclude_from_search = false, so I am at a complete loss here, as to why I cannot get the event post type included in any search.

Just for clarification

Here is an example query string

  ?s=science&post_start_date=&post_end_date=&post_type[]=event

and the result of $wp_query->query_vars is

array(6) { ["post"]=> string(4) "post" 
           ["page"]=> string(4) "page" 
           ["attachment"]=> string(10) "attachment" 
           ["features"]=> string(8) "features" 
           ["briefs"]=> string(6) "briefs" 
           ["campus_news"]=> string(11) "campus_news" 
 } 

Where's my event post_type?

1
  • Just for clarification... Here is an example query string "?s=science&post_start_date=&post_end_date=&post_type[]=event" and the result of $wp_query->query_vars is array(6) { ["post"]=> string(4) "post" ["page"]=> string(4) "page" ["attachment"]=> string(10) "attachment" ["features"]=> string(8) "features" ["briefs"]=> string(6) "briefs" ["campus_news"]=> string(11) "campus_news" } Where's my event post_type?
    – cj5
    Commented Jul 31, 2012 at 21:53

1 Answer 1

1

build the url parameters something like this :

?post_type[]=event&post_type[]=feature&post_type[]=brief

Then $searchposttypes = $_GET['post_type'] will have an array. pass it on to the wp_query

i should add that you should validate the $_GET parameters before going on with it.

reference : wp_query - type parameters

6
  • Okay =) so the question is how do I represent multiple post types in a query_var string?
    – cj5
    Commented Aug 1, 2012 at 18:38
  • when you build your wp_query's "$args" array, pass another array to post_type. i've updated my response.
    – pcarvalho
    Commented Aug 1, 2012 at 18:48
  • Sorry, I was not clear, but I am using a query string in the URL, not as a PHP array, and I cannot use a WP_Query object because I need the data from user interaction, to check certain checkboxes, for a search filter. Can I use &post_type=event,feature,brief,...?
    – cj5
    Commented Aug 1, 2012 at 19:01
  • ohh i completly missunderstood you. sorry about that. Build the query string like .. better yet, i'll change the response again.
    – pcarvalho
    Commented Aug 1, 2012 at 19:47
  • Hmmm. That's pretty much where I started in asking this question. It doesn't work =/
    – cj5
    Commented Aug 2, 2012 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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