The core issue is that you’re having trouble creating a filtered search. Why not use a pre_get_posts
hook to modify the query before it actually gets called, then you don’t need to modify the template files at all:
/**
* Modify WP_Query before it asks the database what data to retrieve
* Belongs in functions.php
*
* @param WP_Query Object $query
*
* @return void
*/
function wpse_265499( $query ) {
// Don't run on admin
if( $query->is_admin ) {
return;
}
// IF main query and search page
if( $query->is_main_query() && $query->is_search() ) {
// IF we have our post type array set
if( isset( $_GET, $_GET['post_type'] ) && ! empty( $_GET['post_type'] ) ) {
$query->set( 'post_type', $_GET['post_type'] ); // Will be set as boutique
}
}
}
add_action( 'pre_get_posts', 'wpse_265499' );
The above would be added to your functions.php
file and what pre_get_posts
does is before it goes to the database and grabs the data to be displayed, we can modify what is being requested. In this case we’re saying IF we have post_types
set in the URL as a $_GET
, grab only those post types. So your main query will now only show the post types assigned in $_GET
at which point there’s no need for the conditional statements in your template.
Let’s break this down starting with the error itself.
array_key_exists() expects parameter 2 to be array, string was given
So based on the error we know that array_key_exists()
needs parameter 2 to be an array, so that it can search for a key, but at some point a string was given instead of an array.
As far as I can tell you only have one instance of array_key_exists()
and it looks like this:
array_key_exists( $post_type , $query_types )
Combined with the PHP error we can surmise that $query_types
must be a string instead of the array that we need for array_key_exists()
to work. Let’s look at how $query_types
is set:
$query_types = get_query_var( 'post_type' );
If we look at the get_query_var()
function we see it can return an array because the return type is mixed. That being said, we’re only asking for one post_type
so it’s probably returning a string for the queried post type.
I’m not sure what it is you’re trying to accomplish but hopefully that clears up the reason you’re getting that error.