0

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!

0

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.

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.