You can’t do that with the wp_get_archives function. It doesn’t work that way.
To do what you want you’ll need to use a couple filters and modify the SQL queries it’s doing. Something sorta like this:
function customarchives_join($x) {
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)";
}
add_filter('getarchives_join','customarchives_join');
function customarchives_where($x) {
$exclude='1'; // cat id to exclude
return $x." AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
}
add_filter('getarchives_where','customarchives_where');
Thread Starter
Erik
(@buckycat7)
Okay so would I place that in the functions file and then call <?php getartchives_where(); ?> ?
No, you would place that (or something like it, I haven’t tested that) in the functions file and then call wp_get_archives() as per normal.
Thread Starter
Erik
(@buckycat7)
I’m not that great at PHP, I understand the basics and can code most of a WordPress theme, however I placed that code into my functions file and now no post are showing up. Could I get a little help debugging it?
Thread Starter
Erik
(@buckycat7)
I have the archives problem figured out.
Any thoughts on what the Conditional Tag won’t work with the blog page?
The solution given by Otto42 above requres a small change in order to work: each function needs to access the global wpdb variable. Here’s the amended code:
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)";
}
Thank you Otto42, thank you lumpysimon. That’s exactly what I was looking for. Works much better than my tries excluding a bunch of posts by IDs with the “getarchives_where” filter only…
And you can even exclude more than one category:
$exclude = ‘1,5’; // category ids to exclude
brilliant!
KS
(@karl19)
Great, this was exactly what we’d been looking for as well – thanks for sharing!
Hi. This is exactly what I was looking for. I want to know if is possible to use conditional if statements with this function. I will clarify what I want to do:
I have a parent category named as “News” with 2 child categories “esp” (for spanish posts) and “eng” (for english post). So…I’m managing what type of posts to show from “News” category depending of which is the current language SESSION. If session = esp, I display (with a query_post call) only post from “esp” child category, else if session = eng, I display posts from “eng” child category. What I want to know is if is possible to use the excluding category function as this way:
if session = esp, so exclude “eng” post
else if session = eng, exclude “esp” post.
That’s the first thing that I want to attemp. Once I could resolve this, I have to research how to show wp_get_archives in english months format por “eng” and in spanish month format for “esp”.
Thanks in advance!
ps: sorry for my bad english.
Hi
I can’t get this to work – am I right in putting it into wp-includes/functions.php? Is there a correct place to put it?
Thanks
Hi juliageek,
the trick with changing the behavior of wordpress is, that you never change any file of your WP installation in the wp-admin or the wp-includes folder.
All changes are made in wp-content
- either in your theme: wp-content/themes/my_theme/functions.php
- or in a plugin you have installed: wp-content/plugins/my_plugin/my_functions.php
Hi,
I’m looking to expand this slightly but I’m not sure how to do it.
Basically I’d like to to pass the categories to include rather than exclude, also i’d like to be able to get it to show archives for ‘future’ posts as well (as I am trying to use it for managing events within a custom template – sidebar-gigs.php)
Somehing like:
wp_get_archives('type=monthly&category=11,12&post_status=published,future');
Can anyone help?
I found this thread and I am also interested in including instead of exluding
Hi,
Thankyou, Thankyou, Thankyou for this..its superb
The following code will only display a specific category in the archive list.
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)";
$include = '1'; // category id to include
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}
Can someone please explain the exact usage/implementation of this.
thank you!