web-dev-qa-db-fra.com

Comment puis-je simuler "taxonomy__in" dans la requête?

Comment puis-je avoir à définir tax_query pour obtenir des résultats comme 'category__in' => array ()?

Plus précisément, je voudrais afficher tous les messages qui ont un terme de la taxonomie de la ville avec l'un de ces identifiants: $cities = array(23,34,45,56);

C'est le code que j'utilise actuellement.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'meta_query' => array(
    array(
            'key' => 'mar_bedrooms',
            'value' => $bedroomss,
            'compare' => 'IN'
        ),
    array(
            'key' => 'mar_property_for',
            'value' => $property_forss,
            'compare' => 'IN'
        ),
    array(
        'key' => 'mar_price',
        'value' => array( $price_min, $price_max ),
        'type' => 'numeric',
        'compare' => 'BETWEEN'
        )
    ),
    'post_status'=>'publish',
    'post_type'=>'post',
    //'cat' => 1,
    'category__in'=> $type,
    //'taxonomy__in'=> $city,
    'orderby'=>'date',
    'order'=>'DESC'
);
query_posts($args);

if ( have_posts() ) {
    the_post();
    $a = 1;
}
1
Marius

Vous ne pouvez pas, en raison de la façon dont les catégories et les taxonomies générales fonctionnent. Les catégories sont un type de taxonomie, nous interrogeons donc un niveau plus bas lorsque nous recherchons des catégories. Lorsque vous recherchez category__in => array(), il recherche en fait ce que category_terms sont interrogés et interrogent les publications de toutes ces catégories. Maintenant, cet effet, nous pouvons l'imiter.

$terms_in = array(23,34,45,56);
$taxonomy_terms = get_terms( array( 'city' ), array(
    'orderby' => 'none',
    'hide_empty' => 1,
    'include' => $terms_in
) );

foreach ( $taxonomy_terms as $term ) :
    $args = array(
        'taxonomy' => $term->slug,
        'post_status' => 'publish',
        'posts_per_page' => -1,
    );

    $term_name = $term->slug;
    $query = new WP_Query( $args );
    while( $query->has_posts() ) : 
        $query->the_post();
        // DISPLAY HERE
    endwhile;

endforeach;

wp_reset_postdata();

Le code ci-dessus a été modifié pour votre question spécifique.

2
Derk-Jan

Après 3 jours de recherche, j'ai trouvé le bon moyen de simuler taxonomy_in.

      $args = array(
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'key' => 'mar_bedrooms',
                'value' => $bedroomss,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_bathrooms',
                'value' => $bathroomses,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_property_for',
                'value' => $property_forss,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_price',
                'value' => array( $price_min, $price_max ),
                'type' => 'numeric',
                'compare' => 'BETWEEN'
            )
        ),
        'tax_query' => array(
            array(
                'taxonomy' => 'city',
                'field' => 'id',
                'terms' => $city,  //$city can be also an array
                'operator' => 'IN'
            )
        ),
        'post_status'=>'publish',
        'category__in'=> $type,  //$type can be also an array
        'orderby'=>'date',
        'order'=>'DESC'
    );

    $the_query = new WP_Query( $args );
        if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
wp_reset_query();
wp_reset_postdata();
1
Marius