I've been stuck on this for hours... I did a var_dump($list_items) right before if ( $list_items ) { and it turns up empty, but a var_dump($hold_tax_query) looks (to me) like it should.

if($selected_custom_taxonomies){

$hold_tax_query = array('relation' => 'OR');

foreach ( $selected_custom_taxonomies as $a_tax ) :     
array_push($hold_tax_query, array('taxonomy' => 'customtax', 'field' => 'slug', 'terms' => $a_tax->slug));
    endforeach; 

$list_items = get_posts(
    array(
        'posts_per_page' => -1,
        'post_type' => 'custompost',
        'tax_query' => $hold_tax_query,
    )
);
//never gets past here because $list_items is empty...
if ( $list_items ) {

    foreach ( $list_items as $post ) :
        setup_postdata( $post ); 
?>
        <h2></h2>
        <?php the_field('description'); ?>
 <?php
    wp_reset_postdata();
endforeach; 
}
}
?>

Something is wrong with the array $hold_tax_query because when I only use 1 taxonomy field, it retrieves the custom post type...

Update:

Here is the output of the $hold_tax_query after the array_push loop and 3 tags were selected.

echo '<pre>' . var_export($hold_tax_query , true) . '</pre>';

The output:

array (
  'relation' => 'OR',
  0 => 
  array (
    'taxonomy' => 'listcat',
    'field' => 'slug',
    'terms' => 'mark',
  ),
  1 => 
  array (
    'taxonomy' => 'listcat',
    'field' => 'slug',
    'terms' => 'school',
  ),
  2 => 
  array (
    'taxonomy' => 'listcat',
    'field' => 'slug',
    'terms' => 'testeritem',
  ),
  3 => 
  array (
    'taxonomy' => 'listcat',
    'field' => 'slug',
    'terms' => 'testtt',
  ),
)
  • Are you sure that $hold_tax_query is an array of arrays? The correct format is $hold_tax_query = array( array('taxonomy' => 'customtax', ...), array(...) ) – Laxmana May 25 '17 at 11:05
  • Sorry, it seems right. Just saw the initiation of $hold_tax_query – Laxmana May 25 '17 at 11:06
  • @Laxmana I'm assuming that's where the problem must lie... but I can't figure out what I did wrong – jonbon May 25 '17 at 11:10
  • Could you craft the query yourself to see if it's working? – Laxmana May 25 '17 at 11:49
  • 1
    The global $wp_query will not contain the results of get_posts, if you want to inspect the query you need to use WP_Query. – Milo May 25 '17 at 12:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.