I want exclude the particular category in sidebar

In my sidebar i would like show archieves list month exclude ‘banner’ category post. Below codes are tried, but not work for me.

  <?php echo wp_get_archives('type=monthly&exclude=6&limit=5'); ?>


  <?php echo wp_get_archives( array( 'type' => 'monthly', 'exclude' => 6,'limit' => 5) ); ?> 

   <?php $args = array(
'type'            => 'monthly',
'limit'           => '',
'format'          => 'html', 
'before'          => '',
'after'           => '',
'show_post_count' => false,
'echo'            => 1,
'order'           => 'ASC',
'month'             => -6
 ); 
  echo wp_get_archives( $args ); ?>  

I added those code in sidebar.php

Solutions Collecting From Web of "I want exclude the particular category in sidebar"

Notice that you don’t need echo to display the result, since echo=1 is the default settings of wp_get_archives().

As @PieterGoosen explained, the wp_get_archives() function doesn’t support the exclude parameter.

But we can use _exclude_terms, the custom parameter of the wp_get_archives() function, to exclude posts with some given terms.

Here’s an example:

/**
 * Exclude terms from the wp_get_archives() function.
 */
wp_get_archives( 
    array( 
        'type'           => 'monthly', 
        '_exclude_terms' => '21,22',     // <-- Edit this to your needs!
        'limit'          => 5
    ) 
);

where we use the following plugin to support this custom parameter:

<?php
/**
 * Plugin Name:   Enhance the wp_get_archive() function.
 * Description:   Support the '_exclude_terms' parameter.
 * Plugin URI:    https://wordpress.stackexchange.com/a/170535/26350
 * Plugin Author: birgire
 * Version:       0.0.1
 */

add_action( 'init', function() {
        $o = new WPSE_Archive_With_Exclude;
        $o->init( $GLOBALS['wpdb'] );
});

class WPSE_Archive_With_Exclude
{
    private $db = null;

    public function init( wpdb $db )
    {
        if( ( $this->db = $db ) instanceof wpdb )
            add_filter( 'getarchives_where', 
                array( $this, 'getarchives_where' ), 10, 2 );
    }

    public function getarchives_where( $where, $r )
    {                                                               
        if( isset( $r['_exclude_terms'] ) )
        {   
            $_exclude_terms = $r['_exclude_terms'];

            if( is_string( $_exclude_terms ) )
                $_exclude_terms = explode( ',', $_exclude_terms );

            if( is_array( $_exclude_terms ) )
                $where .= $this->get_excluding_sql( $_exclude_terms );   
        }
        return $where;
    }

    private function get_excluding_sql( Array $terms )
    {
        $terms_csv = join( ',', array_map( 'absint', $terms ) );

        return " AND ( {$this->db->posts}.ID NOT IN 
            ( SELECT object_id FROM {$this->db->term_relationships} 
            WHERE term_taxonomy_id IN ( $terms_csv ) ) )";
    }

} // end class

Notice that here we use the _exclude_terms parameter, just in case the core will support the exclude parameter in the future.

The _exclude_terms parameter can be a string:

'_exclude_terms' => '21,22',             // <-- Edit this to your needs!

or an array:

'_exclude_terms' => array( 21, 22 ),     // <-- Edit this to your needs!

If you want to use the plugin to exclude some terms from the first native Archive widget, you can use it with:

/**
 * Exclude terms from the first Archive widget.
 */

add_filter( 'widget_archives_args', 'wpse_archive_exclude_terms' );

function wpse_archive_exclude_terms ( $args )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $args['_exclude_terms'] = '21,22';    // <-- Edit this to your needs!
    return $args;
}

or similar for the dropdown case with the widget_archives_dropdown_args filter.

Hopefully you can adjust this to your needs.

Your code won’t work as there are no exclude parameter in wp_get_archives(). Here is the list of valid parameters and their defaults

<?php $args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'html', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC'
); ?>

To accomplish what you want, you will need to change the SQL query inside the function. There are two filter supplied to change the SQL query, getarchives_where and getarchives_join

You can try something like this (Original code taken from here)

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $join ) {

    global $wpdb;

    return $join . " INNER JOIN $wpdb->term_relationships 
                     ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
                     INNER JOIN $wpdb->term_taxonomy 
                     ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $where ) {

    global $wpdb;

    $exclude = '1'; // category id to exclude

    return $where . " AND $wpdb->term_taxonomy.taxonomy = 'category' 
                      AND $wpdb->term_taxonomy.term_id 
                      NOT IN ($exclude)";

}

You will most probably want to remove these filters when you are done, for example

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

wp_get_archives(); 

remove_filter( 'getarchives_where', 'customarchives_where' );
remove_filter( 'getarchives_join', 'customarchives_join' );

After researching for this for hours. This code block worked for me.

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {
    global $wpdb;
    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}

function customarchives_where( $x ) {
    global $wpdb;
    $include = '8'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}