WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Archives by month, excluding a category (5 posts)

  1. ianatkins
    Member
    Posted 5 years ago #

    Hi All,

    I have a category of a site I'm working on that should be distinct from the main blog area. I needed to display an archive list that excluded this category.

    This is what I came up with,

    <ul>
    <li><h2>Archives</h2></li>
    <?php
    $querystr = "SELECT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code> , count(ID) as posts FROM $wpdb->posts 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) WHERE $wpdb->term_taxonomy.term_id != 12 AND $wpdb->term_taxonomy.parent != 12 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
    
    $years = $wpdb->get_results($querystr);
    
    foreach ( (array) $years as $year ) {
    	$url = get_month_link( $year->year, $year->month );
    	$date =mysql2date('F o', $year->year.'-'.$year->month, $translate = true);
    	echo get_archives_link($url, $date, 'html','<li>',' (' . $year->posts .')</li>');
    }
    ?>
    </ul>

    Let me know if I've just wasted and hour and theres and easier solution!

    Change $wpdb->term_taxonomy.term_id != 12 to the ID of the category to exclude, and also $wpdb->term_taxonomy.parent != 12 to the ID of the category for it to apply to subcategories.

    Hope someone finds it useful.

    {P.S The wp get archives function should have an exclude parameter!)

  2. ianatkins
    Member
    Posted 5 years ago #

    <ul>
    <li><h2>Archives</h2></li>
    <?php
    $querystr = "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month' , count(ID) as posts FROM $wpdb->posts 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) WHERE $wpdb->term_taxonomy.term_id != 12 AND $wpdb->term_taxonomy.parent != 12 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
    
    $years = $wpdb->get_results($querystr);
    
    foreach ( (array) $years as $year ) {
    	$url = get_month_link( $year->year, $year->month );
    	$date =mysql2date('F o', $year->year.'-'.$year->month, $translate = true);
    	echo get_archives_link($url, $date, 'html','<li>',' (' . $year->posts .')</li>');
    }
    ?>
    </ul>

    To code messed up above.

  3. sarahd
    Member
    Posted 4 years ago #

    Thank you ianatkins-- this was very helpful! You just made my morning's work a few hours shorter. Guess I can go have another cup of coffee...

  4. Brookfresh
    Member
    Posted 4 years ago #

    hi guys, ive found this post after an exaustive couple of hours - is there a way to limit the archive to a specific category and its sub categories. My client wants both news and blog sections on his site and archives on both.

    thanks in advance

  5. petrusfajarsubekti
    Member
    Posted 4 years ago #

    Hmmm... i don't know why it has to exclude the category.
    does it have good effects?

Topic Closed

This topic has been closed to new replies.

About this Topic