How to exclude categories and tags from your widgets

Sometimes you may need to hide certain tags and categories from your widgets. In this post, I will explain you how to filter out 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, you should change the id list, so 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.

Hope you enjoyed it.


NS Weight Release