1. Home
  2. How To
  3. WP Hacks

[How to] Exclude Categories and Tags From Widgets

How2Buy // (Updated: )

Sometimes you may need to hide certain tags and categories from your widgets. In this post, I will explain you how to hide unwanted terms using wordpress filters.

First open your functions.php file in your theme folder. This is the main script file where we can add our customizations.

Now add this block of code in your functions file:

function shailan_filter_terms( $exclusions, $args ){
	// IDs of terms to be excluded
	$exclude = "3,257"; // CHANGE THIS TO IDs OF YOUR TERMS
	// Generation of exclusion SQL code
	$exterms = wp_parse_id_list( $exclude );
	foreach ( $exterms as $exterm ) {
			if ( empty($exclusions) )
					$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
					$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
	// Closing bracket
	if ( !empty($exclusions) )
		$exclusions .= ')';
	// Return our SQL statement
	return $exclusions;

// Finally hook up our filter
add_filter( 'list_terms_exclusions', 'shailan_filter_terms', 10, 2 );

After you add the code, don’t forget to update the exclude id list in the code. That way, it will exclude correct categories or tags from your widgets.

Remember to SAVE and UPLOAD your file after you edit it.
Now your widgets will automatically exclude those terms.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share Share Tweet