WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_archives(cat=id) <— Any way to specifiy cat id in archives? (4 posts)

  1. matrym
    Member
    Posted 3 years ago #

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

  2. David Calhoun
    Member
    Posted 3 years ago #

    I've been looking for an answer to this as well. Kwebble does actually let you include a specific category:

    <?php wp_get_archives('cat=1'); ?>

    The thing I don't like about Kwebble's plugin is that it requires you to disable canonical URLs. WordPress really needs to implement include/exclude arguments into the wp_get_archives() function.

    I haven't been able to find a good solutions out there.

  3. Frumph
    Member
    Posted 3 years ago #

    If you're interested in doing a dropdown this is what I did, you can pretty much do anything with it.

    <?php $temp_query = $wp_query; ?>
    <div class="archive-dropdown-wrap">
    		<select name="archive-dropdown" class="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    			<option value=""><?php echo attribute_escape(__('Archives...','myTheme')); ?></option>
    <?php
    				$thisArchive = new WP_Query(); $thisArchive->query('showposts=-1&cat=2,3,4');
    				while ($thisArchive->have_posts()) : $thisArchive->the_post() ?>
    					<option value="<?php echo get_permalink($post->ID) ?>"><?php the_title(); ?></option>
    				<?php endwhile; ?>
    		</select>
    	</div>
    <?php $wp_query = $temp_query; $temp_query = null; ?>
  4. matrym
    Member
    Posted 3 years ago #

    Thanks for the response Frumph. It's my understanding that what you've got will list posts / titles. My goal is to list months, with a post count, similar to the wp_get_archives function.

    Does anyone else have any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic