Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glueckpress/9804166 to your computer and use it in GitHub Desktop.
Save glueckpress/9804166 to your computer and use it in GitHub Desktop.
[WordPress] Mini plugin to exclude any given categories from queries and widgets. The first file is not part of the plugin, but is included to demonstrate the differences in applying $query->set() instead its wrapper function set_query_var().
<?php
/**
* Exclude categories from prev/next post links.
*
* $exclude (array) - category slugs to retrieve IDs from
* $excluded (array) - category IDs to be excluded
*/
$exclude = array();
$excluded = array();
// Mini plugin active?
if( function_exists( 'glckprss_exclude_categories__category_names' ) ) {
$exclude = glckprss_exclude_categories__category_names();
else {
$exclude = array(
get_category_by_slug( 'my-category' ),
get_category_by_slug( 'my-other-category' )
);
}
// Retrieve IDs
foreach( $exclude as $category ) {
if( $category )
$excluded[] = absint( $category->term_id );
}
/* Next Post */
next_post_link( '%link', '%title', false, $excluded );
/* Previous post */
previous_post_link( '%link', '%title', false, $excluded );
<?php
/**
* Does NOT apply to the Recent Posts widget.
*/
function glck1403271109_exclude_categories( $query ) {
$excluded = array( '1', '2' );
if( ! is_admin() )
set_query_var( 'category__not_in', $excluded );
}
add_filter( 'pre_get_posts', 'glck1403271109_exclude_categories' );
/**
* Does apply to the Recent Posts widget.
*/
function glck1403271122_exclude_categories( $query ) {
$excluded = array( '1', '2' );
if( ! is_admin() )
$query->set( 'category__not_in', $excluded );
}
add_filter( 'pre_get_posts', 'glck1403271122_exclude_categories' );
<?php
/**
* Plugin Name: Mini Plugin: Exclude Categories
* Description: Exclude categories from WordPress queries and widgets.
* Version: 2014.02
* Author: Caspar Hübinger
* Author URI: http://glueckpress.com/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Configuration: category names to be excluded.
*/
function glckprss_exclude_categories__category_names() {
return array(
'my-category',
'my-other-category',
);
}
/**
* Load plugin.
*/
function glckprss_exclude_categories() {
// Exclude categories.
add_filter(
'pre_get_posts', // …from main query
'glckprss_exclude_categories__exclude_query_categories'
);
add_filter(
'widget_categories_args', // …from category widget
'glckprss_exclude_categories__exclude_widget_categories'
);
add_filter(
'widget_categories_dropdown_args', // …from dropdown category widget
'glckprss_exclude_categories__exclude_widget_categories'
);
}
add_action( 'plugins_loaded', 'glckprss_exclude_categories' );
/**
* Get IDs for categories to be excluded.
*
*/
function glckprss_exclude_categories__excluded_categories() {
// Category names to be excluded.
$names = glckprss_exclude_categories__category_names();
$excluded = array();
foreach( $names as $name ) {
$ob = get_category_by_slug( $name );
$id = $ob ? absint( $ob->term_id ) : '';
$excluded[] = $id;
}
return $excluded;
}
/**
* Exclude categories from queries.
*
*/
function glckprss_exclude_categories__exclude_query_categories( $query ) {
$excluded = glckprss_exclude_categories__excluded_categories();
if( ! is_admin() )
$query->set( 'category__not_in', $excluded );
}
/**
* Exclude categories from category widget.
*
*/
function glckprss_exclude_categories__exclude_widget_categories( $args ){
$excluded = glckprss_exclude_categories__excluded_categories();
$args[ 'exclude' ] = $excluded ? $excluded : '';
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment