Pages

Thursday, March 3, 2016

Get Posts of taxonomy Who has no child catgories and the posts has children categories

$catId = $_GET['brandID'];
    $termChildren = get_term_children($catId, 'carproducer');
    //print_r($termChildren);
    if(count($termChildren) > 0 ){
            $args = array(
                'posts_per_page'   => 1,
                'orderby'          => 'date',
                'order'            => 'ASC',
                'post_type'        => 'car',
                'post_status'      => 'publish',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'carproducer',
                        'field' => 'term_id',
                        'terms' => $catId
                    ),
                    array(
                        'taxonomy' => 'carproducer',
                        'field' => 'id',
                        'terms' => $termChildren,
                        'operator' => 'NOT IN'
                    )
                )
             
            );
        }else{
            $args = array(
                'posts_per_page'   => 1,
                'orderby'          => 'date',
                'order'            => 'ASC',
                'post_type'        => 'car',
                'post_status'      => 'publish',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'carproducer',
                        'field' => 'term_id',
                        'terms' => $catId
                    )
                )
             
            );

        }
 $posts_array = get_posts( $args );

No comments:

Post a Comment