R Rumble
R Rumble

Reputation: 1

tax_query with custom taxonomy not working as expected

I'm hoping someone can help spot what the problem is. I have a custom taxonomy called "events_cat" I am trying to get all the post types "event" that are in the taxonomy term 11 to display, however the below code is pulling in events that are not in that taxonomy term and I can't see the mistake. Any ideas what might be causing the issue?:

<?php 
    $args = array(
            'post_type'        => 'event',
            'posts_per_page'   => -1,
            'tax_query'        => array(
                                   'relation' => 'AND',
                                   array(
                                        'taxonomy' => 'events_cat',
                                        'field'    => 'term_id',
                                        'terms'    => array(11),
                                    ),
            ));
    $upcomingEvents = new WP_Query($args); ?>

Upvotes: 0

Views: 972

Answers (1)

Shital Marakana
Shital Marakana

Reputation: 2887

$custom_args=array(
              'post_type'   => "event",
              'post_status'     => 'publish',
              'posts_per_page'  => -1, 
              'caller_get_posts'=> -1,
              'hierarchical'    => 1,
              'exclude'         => '',
              'include'         => '',
              'number'          => '',
              'tax_query'       => array(
                                        array(
                                            'taxonomy' => 'events_cat',
                                            'field' => 'id',
                                            'terms' =>"11"
                                        )
                                    ),
             'orderby'          => 'id',
             'order'            => 'ASC'
            );
            $custom_my_query = null;
            $custom_my_query = new WP_Query($custom_args);
            $custom_my_total_count = count($custom_my_query);
            if( $custom_my_query->have_posts() ) 
            {
                    while ($custom_my_query->have_posts()) : $custom_my_query->the_post(); 
                        ?>
                        <a href="<?php echo get_permalink();?>"><?php echo get_the_title($post->ID);?></a>
                        <?php
                      endwhile;
            }
            wp_reset_query($custom_my_query);  // Restore global post data stomped by the_post().
    }

Upvotes: 1

Related Questions