0

I want get all the post type st_tours associated to the taxonomy st_tour_type, when I browse the tour categories in the dashboard I have this link:

taxonomy=st_tour_type&post_type=st_tours

so I wrote this query:

   $args = array(
        'post_type'     => 'st_tours',
        'post_status'   => 'publish',
        'tax_query'     => array(
            'taxonomy'  => 'st_tour_type'
        ),
        'order'         => 'DESC'
    );

    $query = new WP_Query($args);
    var_dump($wpdb->found_posts);
    var_dump($wpdb->last_query);

but this return me just one post, and the query executed is actually incorrect 'cause I have:

 SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (6565) ORDER BY meta_id ASC

what I did wrong?

UPDATE

$args = array(
    'post_type'     => 'st_tours',
    'post_status'   => 'publish',
    'tax_query'     => array(
        array(
            'taxonomy'  => 'st_tour_type'
        )
    ),
    'order'         => 'DESC'
);

$query = new WP_Query($args);

if ($query->have_posts()) : ?>
    <?php the_title(); ?>

    <?php echo 'test' ?>

<?php endif;

nothing is printed out

1 Answer 1

1

Seems like you have a missing array in tax_query

instead of

'tax_query'     => array(
    'taxonomy'  => 'st_tour_type'
),

should be

'tax_query'     => array(
    array (
        'taxonomy'  => 'st_tour_type',
        'operator' => 'EXISTS'
    )
),

When using tax query without terms/fields we must include the "operator" property.

You can check WP_Query taxonomy for more information on working with taxonomies.

9
  • Hi, thanks for the answer. I tried your solution but it return NULL on $wpdb->post_count
    – sfarzoso
    Feb 4, 2021 at 11:47
  • Why are you checking $wpdb? the object will be in $query, you created a new instance of WP_Query and assigned it to $query Feb 4, 2021 at 11:49
  • I also tried to add an echo 'test'; inside ($query->have_posts()) but nothing is displayed
    – sfarzoso
    Feb 4, 2021 at 12:08
  • Could you update the question code to show all the new changes that you tried? Feb 4, 2021 at 12:09
  • yes, I have updated, thank you
    – sfarzoso
    Feb 4, 2021 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.