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 having trouble with getting tax_query to act as a filter for posts. What I would like is to have it so it will display cpt's that have the terms of county and status but also work if terms are not provided for status or county. So have the posts show if it has the term of "Adams" county in the County taxonomy and the the term of "Locked" in the Status taxonomy. But it needs to also work if a County is not provided, just a status and vice-versa.

Here is my code:

    <?php
    $args = array(
        'post_type' => 'property',
        'posts_per_page' => 5000,
        'tax_query' => array(
            'relation' => 'AND',
            array(
            'taxonomy' => 'Status',
            'field' => 'slug',
            'terms' => explode( ',', $st_term_final_string    )
            ),
            array(
            'taxonomy' => 'County',
            'field' => 'slug',
            'terms' =>  explode( ',', $il_term_final_string )
            )
        )
    );
    $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;
    ?>

Any hints to put me in the right direction?

Thanks in advance

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Change your code with the following:

$args = array(
  'post_type' => 'property',
  'posts_per_page' => 5000,
);

$statuses = explode( ',', $st_term_final_string);
$terms = explode( ',', $il_term_final_string );

if ( $statuses || $terms ) {
  $args['tax_query'] = array();
  if ( $statuses ) {
    $args['tax_query'][] = array(
      'taxonomy' => 'Status',
      'field' => 'slug',
      'terms' => $statuses
    );
  }

  if ( $terms ) {
   $args['tax_query'][] = array(
     'taxonomy' => 'County',
     'field' => 'slug',
     'terms' => $terms
    );
  }

  if ( $statuses && $terms ) $args['tax_query']['relation'] = 'AND';

  $loop = new WP_Query( $args );

  // do your stuff here

} else {

  // You have no statuses nor county, I don't know what you want to do in this case

}

Hope it helps.

share|improve this answer
    
I've tried variations of this along with what you posted but I can't get it to work. Just to test, I tried echoing out "testing" and I put it in each condition and it shows no matter what. Not sure what I might have missed. –  graphical_force Jul 23 '13 at 1:23
    
have you tried to var_dump( explode(',', $st_term_final_string) ); and $il_term_final_string ? –  G. M. Jul 23 '13 at 1:25
    
Yes, I tried those and they are showing the correct data. I'm just not seeing anything when it loops through with the $args. –  graphical_force Jul 23 '13 at 2:20
    
Got it working. I had an issue that was being caused by some code that was not posted. Thanks! –  graphical_force Jul 23 '13 at 6:15

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.