简体   繁体   中英

Exclude specific taxonomy from query

i have a problem, in first this is my code

$page_id = get_queried_object_id();
$post = get_post($page_id);
$filter_args = array(
    'tax_query'=>array(),
    'meta_query'=>array()
);

$posts_per_page = 5;
$args = array(
    'posts_per_page' => $posts_per_page,
    'post_type' => 'property',
    'orderby' => 'date'
);

$args = array_merge($args, $filter_args);
$query = new WP_Query( $args );

$items = array();

if(count($query->posts) > 0){
    $items = $query->posts;
}

$acf_fields = get_acf_fields(array('general_property_fields', 'property_price'));
foreach($items as $k=>$v){
    $items[$k]->acf = get_acf_data($v->ID, $acf_fields);
    $items[$k]->pc = sb_get_price($v->ID);
}

The get_acf_fields is a personal function, not worry in this question about that.

And this is my loop for get the values i want.

<?php foreach ($items as $v) { 
    $status = $v->acf['c_status'];
    $status_name = get_term_by('id', $status, 'type');
?>
<?php if ($status_name) { ?>
      <div class="sb-sidebar-featured-type">
      <?php
         $title_status = $status_name->name;
         $status = explode(' ', $title_status); 
      ?>
         <div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
      </div>
<?php } ?>

All work fine, but the status give me all values from the taxonomy with name status, for example if i obtain "sold", "discount", "offer", i want to exlude the results with "sold" in the taxonomy but i dont know how make that, thanks :D

Will you please try like this:
<?php if ($status_name != 'sold') { ?>
      <div class="sb-sidebar-featured-type">
      <?php
         $title_status = $status_name->name;
         $status = explode(' ', $title_status); 
      ?>
         <div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
      </div>
<?php } ?>

Here is the code working for somelse

<?php foreach ($items as $v) { 
    $tax_type = $v->acf['c_type'];
    $status = $v->acf['c_status'];
    $type_name = get_term_by('id', $tax_type, 'type');
    $status_name = get_term_by('id', $status, 'type');
    if ($status_name) {
        $check_status = trim($status_name->slug);
    }
    if ($check_status != 'sold' && $check_status != 'vendido') { 
?>
        <div class="sidebar-featured-cont">
            <div class="sidebar-featured">
                <a class="sidebar-featured-image" href="<?=get_permalink($v->ID)?>">
                    <img src="<?=$v->acf['c_gallery'][0]['sizes']['property-listing']?>" alt="" />
                    <?php if ($status_name) { ?>
                        <div class="sb-sidebar-featured-type">
                            <?php
                                $title_status = $status_name->name;
                                $status = explode(' ', $title_status); 
                            ?>
                            <div class="sidebar-featured-transaction"><?=($status_name) ? $status[0] : '';?></div>
                        </div>
                    <?php } ?>
                </a>
                <div class="sidebar-featured-title"><a href="estate-details-right-sidebar.html"><?=$v->post_title?></a></div>
                <div class="sidebar-featured-price">$ <?=$v->pc?></div>
                <div class="clearfix"></div>                        
            </div>
        </div>
    <?php } ?>
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM