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'm working on a solution to filter a custom post type by multiple terms in multiple taxonomies.

Here is my code:

<?php $term = "peoria,adams"; ?>

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

        <?php $term = explode(',', $term); ?>

        <?php $term_count = count($term); 
        $array_term = $term[0];
        ?>

        <?php 
            $i = 1;
            while ($i < $term_count) {
                $array_term = "'" . $array_term . "', '" . $term[$i] . "'";
                $i++;
            }
            ?>

        <?php echo $term = "array( ". $array_term . " )"; ?>

        <?php //echo $array_term; ?>

        <?php
            $args = array(
                'post_type' => 'property',
                'tax_query' => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy' => 'Status',
                        'field' => 'slug',
                        'terms' => array( 'Locked' )
                    ),
                    array(
                        'taxonomy' => 'County',
                        'field' => 'slug',
                        'terms' =>  $term
                    )
                )
            );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                the_title();
                echo '<div class="entry-content">';
                echo get_the_post_thumbnail();
                the_content();
                echo '</div>';
            endwhile;
        ?>

I echo $term and it display what it should display but it does not work when I call it in 'terms' => $term it does not work but it will work if I replace it with array( 'peoria', 'adams' ) it works.

I have a suspicion I'm doing something stupid but I'm just missing it. Can someone give me a hand?

Thanks in advance!

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It looks like the problem is here:

'terms' =>  $term

where in your case it's like:

'terms' =>  "array( 'peoria', 'adams' )"

but it should be like

'terms' =>  array( 'peoria', 'adams' )

By looking at your code snippet, you could try

'terms' =>  explode( ',', $term );

since explode will return the array you are looking for.

So you shouldn't construct the array as a string.

Check for example the PHP manual on arrays here and explode here.

share|improve this answer
    
Thanks! that seemed to work! –  graphical_force Jul 22 '13 at 20:14
add comment

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.