Support » Fixing WordPress » wp_get_archives and Conditional Tags

  • I have two quick questions. But here is how WP is being used, we are using it more in a CMS application, with a section for company news and press releases along with a blog. The post page is set to the blog and I’ve removed the news category from showing in in both the feed and blog post page. However I’m trying to add the wp_get_archives tag and can’t figure out how to remove the news category from showing.

    This is what I was trying:

    <?php query_posts("cat=-1"); ?>
    <?php wp_get_archives('type=postbypost&limit=5'); ?>

    Clearly this doesn’t work, so want can I do to exclude cat. 1 from the archives?

    Secondly, I’m using a CSS sprite Menu and using conditional tags to make the sprite turn to an active state when a visitor is on the page or a child page. However the code I am use will not turn the blog menu item to active when someone is one the blog page. Again the blog page is set to the post page in the reading setting in the WP-Admin Dashboard.

    Here is that line of code:
    <li id="blog"><a <?php if ( is_page('blog') || $post->post_parent == '95' ) { echo " class=\"active\""; } ?> href="blog">Blog</a></li>

    Any reason way it just won’t work on that one page?

Viewing 15 replies - 1 through 15 (of 17 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    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)";
    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)";

    Okay so would I place that in the functions file and then call <?php getartchives_where(); ?> ?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    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.

    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?

    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


    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.

    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?

    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


    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:


    Can anyone help?

    I found this thread and I am also interested in including instead of exluding


    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!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘wp_get_archives and Conditional Tags’ is closed to new replies.