Exclude specific category id's from being output

575 views Asked by At

I have the following code on my site that outputs a list of categories for the 'download' post type. I am looking to exclude certain categories from appearing in the output, how do I do this?

<?php
/**
 * Generate list of EDD categories to browse
 */
$args = array(
  'orderby' => 'name',
  'hierarchical' => 1,
  'style' => 'none',
  'taxonomy' => 'download_category',
  'hide_empty' => 0,
  'depth' => 1,
  'title_li' => '',
  // 'parent' => 0
);
$categories = get_categories( $args );

if ( $categories ) {
  ?>
<div class="search-cats">
  <div class="search-cat-text">
    <?php _e( 'Browse by category: ', 'checkout' ); ?>
  </div>
  <nav>
    <?php
    /**
     * Generate list of EDD category links
     */
    foreach ( $categories as $category ) {
      $link = get_term_link( $category, 'download_category' );
      echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $category->name . '</a>';
    }
    ?>
  </nav>
</div>
<?php } ?>
1

There are 1 answers

0
Howard E On

The parameter in your arguments you're looking for is exclude see https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ for details on the args available for this type of query.

$args = array(
  'orderby' => 'name',
  'hierarchical' => 1,
  'style' => 'none',
  'taxonomy' => 'download_category',
  'hide_empty' => 0,
  'depth' => 1,
  'title_li' => '',
  'exclude' => 24,21 /* Array or comma/space-separated string of term IDs 
                        to exclude. If $include is non-empty, $exclude is
                        ignored. Default empty array. */
);
$categories = get_categories( $args );