WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Excluding category from archive month list (getarchives_join filter) (4 posts)

  1. Simon Blackbourn
    Member
    Posted 4 years ago #

    Hi

    I need to exclude a category (ID 36) from the archives, so that months do not show in the wp_get_archives list if they only contain posts from that category. It looks like I need to use the getarchives_join filter, but my SQL is a bit ropey and I've got stuck.

    I've got this in my functions.php:


    add_filter( 'getarchives_join', 'no_cat36_in_archives' );

    function no_cat36_in_archives( $join ) {

    global $wpdb;

    $join = "INNER JOIN {$wpdb->prefix}term_relationships ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id)
    INNER JOIN {$wpdb->prefix}term_taxonomy
    ON ({$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id)
    WHERE 1=1
    AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
    AND {$wpdb->prefix}term_taxonomy.term_id <> 36";

    return $join;

    }

    but now no archive months are showing at all. Can anyone help me out with the SQL I need to exclude category 36 from the archives?

  2. MichaelH
    Member
    Posted 4 years ago #

  3. Simon Blackbourn
    Member
    Posted 4 years ago #

    Hi Michael

    Thanks very much for the pointers. The two functions worked after one small but crucial change: they were both missing global $wpdb;

    I've added a reply to that thread with this change.

    Cheers
    Simon

  4. rumlab
    Member
    Posted 3 years ago #

    Just wanted to add:

    The code posted in the thread does exclude the categories from wp_get_archives, BUT they are still included in the loop in archive.php.

    For example - I have a yearly archive list with show post count. Initially there was 9 posts in 2010, and after excluding a category adding the code to functions.php, there is rightfully only 7 posts in 2010. But clicking the 2010 link, will still show the original 9 posts.

    The Kwebble plugin solved my problem.

    M

Topic Closed

This topic has been closed to new replies.

About this Topic