1

I'm trying to build a real estate search site.

first of all, here is the search form (sorry for german classes and stuff tho):

<form  method="post" action="<?php bloginfo('url');?>/immobilien-suche/">
<?php
$taxonomiesImmo = get_object_taxonomies('immobilien');
$termsImmoBundesland = get_terms($taxonomiesImmo[0]);
?>
    <fieldset name="bundeslaender">
        <input type="checkbox" value="alleBundeslaender">alle Bundesl&auml;nder</input>
        <?php foreach ($termsImmoBundesland as $termImmoBundesland) { ?>
            <label><input type="checkbox" value="<?php echo $termImmoBundesland->slug; ?>" name="checkedBundeslaender[]"><?php echo $termImmoBundesland->name; ?></label>
        <?php } ?>
    </fieldset>
    <input type="submit"/>  
</form>

then in my search result page template I implode those multiple checkbox results (array) to get a clean list, like it's stated in the function reference:

'terms'    => array( 'action', 'comedy' ), // wordpress codex

my implode

if ( count($_POST['checkedBundeslaender']) > 1 ) {
    $checkedBundeslaenderList = "'".implode("', '", $_POST['checkedBundeslaender'])."'";
    // string form: 'term1', 'term2', 'term3' ...
} else {
    $checkedBundeslaenderList = $_POST['checkedBundeslaender']);
    // string form: 'term1'
}

and finally my query args:

$newImmoArgs = array(
    'post_type' => 'immobilien',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'bundesland',
            'field'    => 'slug',
            'terms'    => array( $checkedBundeslaenderList ),
            'operator' => 'IN',
            'include_children' => false,
        ),
    ),                              

);

My problem is, that my query doesn't have any results if there are 2 or more checkboxes checked... its only working when only 1 checkbox is checked.

please help!

best regards, rellston.

2

You're mixing up arrays. In your "my implode", $checkedBundeslaenderList varies between a string and an array, depending on the number of items.

And then in your query args, you nest it in an array:

'terms' => array( $checkedBundeslaenderList ),

So what you could end up with is either:

array( array( 1 ) );

...or:

array( '1,2,3,4' );

Neither are valid formats. Instead, always use an array:

if ( ! empty( $_POST['checkedBundeslaender'] ) ) {
    $checkedBundeslaenderList = wp_unslash( ( array ) $_POST['checkedBundeslaender'] );
} else {
    $checkedBundeslaenderList = array();
}

And then just pass it straight to your query:

'terms' => $checkedBundeslaenderList,
1
  • TheDeadMedic you are my hero! :* works perfectly :) thanks! :D – Rellston May 12 '15 at 18:14

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.