error in Advanced Search Form for Custom Post Types in WordPress array_key_exists() expects parameter 2to be array, string given

i am building an advanced form but the search is showing all post all time with this error too array_key_exists() expects parameter 2to be array, string given

this is my code in header.php

<?php $query_types = get_query_var('post_type'); ?>
<?php

     $args = array(
                 'public'   => true,
                 '_builtin' => false
             );

     $output = 'names'; // names or objects, note names is the default
     $operator = 'and'; // 'and' or 'or'

     $post_types = get_post_types( $args, $output, $operator ); 

     //array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) see http://php.net/manual/en/function.array-slice.php
    foreach ( array_slice($post_types, 0, 5) as $post_type ) { //display max 5 post_type
         ?> 
    <li>
        <a>
           <input id="<?php  echo $post_type ?>" class="btn btn-lg" type="checkbox" name="post_type[]" value="<?php  echo $post_type ?>" <?php if (!empty($query_types) && array_key_exists($post_type , $query_types)) { echo 'checked="checked"'; } ?>/> 
           <!-- array_key_exists($post_type , $query_types) -->
           <label for="<?php  echo $post_type ?>"><span><?php  echo $post_type ?></span></label> 
       </a> 
    </li> 
 <?php 
} 
?>

in searchfrom.php a have this:

<?php
    $postType = array("construction","renovation","agrandissement","bradage");

    foreach ($postType as &$value) {
        echo '<input type="hidden" name="post_type[]" value="'.$value.'" />';
    }
    unset($value); // break the reference with the last element
?>

when i add && is_array($query_types) in header.php array_key_exists() expects parameter 2to be array, string given doesn`t show anymore but the result is empty please help mer figuring out what i am doing wrong thank you

Read more here: error in Advanced Search Form for Custom Post Types in WordPress array_key_exists() expects parameter 2to be array, string given

Leave a Reply

Your email address will not be published. Required fields are marked *