admin管理员组

文章数量:1300049

This topic has been ask in Stackexchange many times. But most of question unanswered or has incomplete answer. My custom post type "Product" and two different taxonomy "Sectors" and "Seasons". Now I want to display product count for Garden archive page ( garden is the term of sectors taxonomy) that have realtion with Winter ( winter is the term of seasons taxonomy). I did all my best during these 3 days but I can not solve this problem. Please help me. thanks for your attention. This is also a good source but can make it works

$sectors = get_queried_object();

$season = get_terms( array(
    'taxonomy' => 'seasons',
    'hide_empty' => true,
    'fields' =>  '58', // winter term id
) );


foreach ($current_taxs as $current_tax) {

foreach ($seasons as $seasons) {

$args = array(
    'post_type' => 'product',
 'post_status'=>'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => $current_tax,
            'field'    => 'slug',
           'terms'    => array( $current_tax ),
        ),
        array(
            'taxonomy' => 'season',
            'field'    => 'slug',
           'terms'    => array( $season ),
        ),
    ),
);
$query = new WP_Query( $args );

echo $query->post_count;

}
}

This topic has been ask in Stackexchange many times. But most of question unanswered or has incomplete answer. My custom post type "Product" and two different taxonomy "Sectors" and "Seasons". Now I want to display product count for Garden archive page ( garden is the term of sectors taxonomy) that have realtion with Winter ( winter is the term of seasons taxonomy). I did all my best during these 3 days but I can not solve this problem. Please help me. thanks for your attention. This is also a good source but can make it works https://wordpress.stackexchange/a/327150/196375

$sectors = get_queried_object();

$season = get_terms( array(
    'taxonomy' => 'seasons',
    'hide_empty' => true,
    'fields' =>  '58', // winter term id
) );


foreach ($current_taxs as $current_tax) {

foreach ($seasons as $seasons) {

$args = array(
    'post_type' => 'product',
 'post_status'=>'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => $current_tax,
            'field'    => 'slug',
           'terms'    => array( $current_tax ),
        ),
        array(
            'taxonomy' => 'season',
            'field'    => 'slug',
           'terms'    => array( $season ),
        ),
    ),
);
$query = new WP_Query( $args );

echo $query->post_count;

}
}
Share Improve this question edited Mar 25, 2021 at 9:47 Teymur Abbasov asked Mar 25, 2021 at 8:28 Teymur AbbasovTeymur Abbasov 618 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

SOLUTION

  <?php
    $args = array(
        'post_type' => 'product',
     'post_status'=>'publish',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'sectors',
                'field'    => 'slug',
               'terms'    => 'garden',
            ),
            array(
                'taxonomy' => 'season',
                'field'    => 'slug',
               'terms'    => 'winter',
            ),
        ),
    );
    $query = new WP_Query( $args );
    
    echo $query->post_count ;
    
    
    
    ?>

本文标签: wp querydisplay post count in archive page that have relation with another taxonomy term