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
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.