从查询中排除特定分类

时间:2016-03-22 18:08:56

标签: php wordpress

我有一个问题,首先这是我的代码

std::unique_ptr

get_acf_fields是一个个人功能,在这个问题上不用担心。

这是我获取我想要的值的循环。

$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);
}

所有工作都很好,但是状态会给我带有名称状态的分类标准的所有值,例如,如果我获得"出售","折扣","提供&# 34;,我想用"出售"排除结果。在分类学,但我不知道怎么做,谢谢:D

2 个答案:

答案 0 :(得分:2)

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 } ?>

答案 1 :(得分:0)

以下代码适用于某些

<?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 } ?>