0

I'm having a strange issue where the output of the following query isn't always correct.

<?php // Get all the categories
$categories = get_terms( 'soort-event', array(
'include' => array( 36,37,44),
) );

foreach ( $categories as $category ):
    
    $meta_query = array(
                      array(
                    'key'     => 'datum_event',
                    'value'   => date('Ymd'),
                    'type'    => 'DATE',
                    'compare' => '>',
                    )
                  ); 

    $events = new WP_Query(
        array(
                'post_type'       => 'events',
                'orderby'         => 'meta_value_num',
                'order'           => 'ASC',
                'posts_per_page'  => '3',  
                'tax_query' => array(
                array(
                    'taxonomy'  => 'soort-event',
                    'terms'     =>  $category,
                    'field'     => 'term_id'
                )
            ),             
                'meta_query'      => $meta_query
          )
     );
    ?>
    
<?php if ($events-> have_posts()):?>

// Display some taxonomy data

    while ($events->have_posts()) : $events->the_post(); ?>
            
       // Display some post data
    
    // End loop 
    endwhile;?>
        
    endif;
        
    // Reset things, for good measure
    wp_reset_postdata();

// end the loop
endforeach;
?>

When I echo $category->term_id within the loop it shows one of three assigned ID's as set by the query (36,37,44). However their actual taxonomy ID is something different. I checked this in the wp_terms_relationship database table.

I don't understand why this happens and only for some specific posts. I'm using the query above on a different template with only some other taxonomy ID's and the output is just fine.

Any help is much appreciated.

3
  • Is the output of $category correct in your tax_query? Does it output just an ID? Can you try to edit the terms parameter in your tax_query, like so: 'terms' => array($category->term_id)
    – DeltaG
    Sep 15 at 10:23
  • Editing the terms parameter solved the issue indeed, thanks! But I only don't understand why the previous parameter didn't work in all cases. Can you explain me why it didn't work?
    – NielsPilon
    Sep 15 at 10:57
  • Good to hear this solved your problem! The reason you had issues with this is because get_terms doesn't just return an ID, it returns the entire term object (unless you specify you only want to return IDs). For more information about the get_terms function and the possible options, you can check out developer.wordpress.org/reference/functions/get_terms
    – DeltaG
    Sep 15 at 11:53

1 Answer 1

0

For anyone visiting this topic in the future, I will form an answer of my comment.

The reason for this issue was the use of $category inside the tax_query. The get_terms function return terms as entire objects by default. The tax_query expects an ID (or an array of IDs). This caused issues with the execution of the tax_query.

For anyone having the same problem, make sure your only including your term IDs. So this:

'tax_query' => array(
    array(
        'taxonomy'  => 'soort-event',
        'terms'     =>  $category,
        'field'     => 'term_id'
    )
)

Becomes this:

'tax_query' => array(
    array(
        'taxonomy'  => 'soort-event',
        'terms'     =>  array($category->term_id),
        'field'     => 'term_id'
    )
)

More about get_terms: https://developer.wordpress.org/reference/functions/get_terms/

More about tax_query: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

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.