• I’m trying to display a list of custom posts by category from a shared custom taxonomy. This issue I am having is if a category is selected in an alternate custom taxonomy then it displays in my list even though it is empty/not used in the custom post that is being queried. I feel like I am really close to solving this but I just can get over the hump. Any help would be greatly appreciated.

    <?php
    // get all the categories from the database
    $cats = get_terms('apprenticeship-type', array('post_type' => 'apprenticeship-res', 'hide_empty' => true, 'parent' => 0));
    $i = 0;
    // loop through the categries
    foreach ($cats as $cat) {
    // setup the cateogory ID
    $cat_id= $cat->term_id;
    // Make a header for the category
    $i++;
    ?>
    <div class="panel">
    <h4 class="panel-title"><a class="collapsed" href="#apprenticeshipDocumentsCollapse<?php echo $i ?>"><?php echo $cat->name?></a></h4>
    <div id="apprenticeshipDocumentsCollapse<?php echo $i ?>" class="panel-collapse collapse">
    <div class="panel-body">
    <?php
    // create a custom wordpress query
    $args = array(
    	'post_type' => 'apprenticeship-res',
    	'tax_query' => array(
    		array(
    		'taxonomy' => 'apprenticeship-type',
    		'field' => 'slug',
    		'terms' => ".$cat->name."
    		)
    	)
    );
    $query = query_posts($args);
    // start the wordpress loop!
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php // create our link now that the post is setup ?>
    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php echo '<hr/>'; ?>
    <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
    <?php wp_reset_query(); ?>
    </div>
    </div>
    </div>
    <?php } // done the foreach statement ?>
  • The topic ‘if term is empty by post type’ is closed to new replies.