views:

240

answers:

1

Does anyone know how to specify an INCLUDE ONLY category using wp_get_archives? I would like to specify a category but then list results by month.

I've tried kwebble's plugin to no avail. I've also found the following on WP forums, but it appears to only exclude categories. Perhaps it can be modified to do include? Even given that, I'm not sure how I would call it...

Thanks in advance!

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;

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

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

Modifying that particular code is somewhat simple ... it's all in the last function's query:

function customarchives_where( $x ) {

    global $wpdb;

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

    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";

It's just a matter of changing the $exclude to $include and omitting the "NOT" keyword in the return query. Rather than returning everything but that category, it will return only that category.

EAMann
Is this something I can put in functions.php? Is there any way to place this within a widget?
Matrym
You can definitely put this code into functions.php, but to build a custom widget would take a lot more work than this.
EAMann