admin管理员组

文章数量:1305038

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

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

Share Improve this question edited Feb 4, 2021 at 12:13 sfarzoso asked Feb 4, 2021 at 11:31 sfarzososfarzoso 498 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: wp queryCannot get custom post