使用Wordpress过滤自定义分类

时间:2013-10-21 09:30:01

标签: wordpress

我一直试图解决这个问题三天,甚至在这个网站上使用解决方案。我仍然无法让这个工作。

我有一个wordpress循环,它使用过滤器按帖子类型显示帖子。现在,帖子类型被称为“案例研究”,因此显示了案例研究中的所有帖子。

但是我需要隐藏这个循环中的特定分类术语。分类法称为“部门”,术语称为“医疗保健”。我尝试了各种组合,但仍然无法得到这个。我需要这个非常紧急。任何可以帮助的人都会挽救我的生命。

这是查询和循环

<?php
// The Query
$the_query = new WP_Query( 'post_type=case-studies&posts_per_page=-1' );

// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post(); 


?>

3 个答案:

答案 0 :(得分:2)

    $args = array(
    'post_type' => 'case-studies',
    'tax_query' => array(
        array(
            'taxonomy' => 'sectors',
            'field' => 'slug',
            'terms' => array('comercial', 'personal', 'etc') //excluding the term you dont want.
        )
    )
);
$query = new WP_Query( $args );

我没有尝试过,但你只能做一个只调用你想要的术语的查询,你之前可以填充列出分类法上所有术语的术语数组,并排除你想要的那些,我认为这是一点点hacky它应该是另一种直接的方式来做它但试一试,因为它是生命或死亡的情况=)。

来源:http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

答案 1 :(得分:1)

试试这个:

<?php
$type = 'cpreviews';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

答案 2 :(得分:0)

好吧然后尝试这个它会动态提取你的术语并排除你不想要的术语,我没有检查它是否有效但这是逻辑,请检查语法错误。

$terms = get_terms("sectors");
$count = count($terms);
$termsAr = array();
if ($count > 0 ){
    foreach ( $terms as $term ) {
        if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
            array_push($termsAr, $term->name);
        }
    }
}

$terms = get_terms("types");
$count = count($terms);
$termsAr2 = array();
if ($count > 0 ){
    foreach ( $terms as $term ) {
        if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
            array_push($termsAr2, $term->name);
        }
    }
}
 $args = array(
    'post_type' => 'case-studies',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'sectors',
            'field' => 'slug',
            'terms' => $termsAr //excluding the term you dont want.
        ),
        array(
            'taxonomy' => 'types',
            'field' => 'slug',
            'terms' => $termsAr2 //excluding the term you dont want.
        )
    )
);
$query = new WP_Query( $args );