WordPress.org

Support

Support » How-To and Troubleshooting » Using wp_get_archives to filter by year?

Using wp_get_archives to filter by year?

  • Hi,

    I am using a yearly dropdown filter (eg. 2009, 2010, 2011) that I want to display all posts written within the applicable year. Can wp_get_archives by used to filter by specific year somehow? If not, is there something else that can do this or will it require a custom function?

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi

    @esmi

    Forum Moderator

    Thank you for the reply, however after reading both those pages it’s still confusing to me. I’m naturally a designer so some of these backend commands are difficult for me to grasp.

    What I’m using now is simple enough <?php wp_get_archives(‘type=monthly’); ?> but it returns months from every year that posts were published.

    I only want something that spits out months of a specific year. I tried doing something simple like:

    <?php $test = query_posts( ‘year=2011’ );
    echo $test
    ?>

    but it returns “Array”. Obviously I don’t know what I’m doing here lol. Can you provide a little more help, would be greatly appreciated!

    esmi

    @esmi

    Forum Moderator

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'year' => 2011,
    	'orderby' => 'date';
    	'paged' => $paged
    );
    query_posts($args);
    ?>

    placed immediately before the Loop will output all posts published during 2011 in date order.

    Esmi,

    Thanks again for posting a suggestion, I really appreciate it.

    However, this doesn’t answer my initial problem. I need it to output just archive links to specific months of a year, not individual posts. Here’s an illustrated example:

    (say I have separate unordered lists for each year, bold text are the archive links)

    2011:
    January 2011
    February 2011
    March 2011
    etc.

    2010:
    January 2010
    February 2010
    March 2010
    etc.

    Again, <?php wp_get_archives(‘type=monthly’); ?> works except it doesn’t allow me any way to distinguish what year to use. It will literally spit out 40 months long going back over 3 years for each of my unordered lists. I’m looking for a function that will only output monthly archive links for a specific year that I can place under each of my lists.

    <ul>...archive function here...</ul>

    I hope this makes more sense. Thanks again.

    Same problem here…

    Can’t find any function, so I use this code:

    global $wpdb, $month;
    
    $years =  $wpdb->get_col("SELECT DATE_FORMAT(post_date_gmt, '%Y')
    FROM $wpdb->posts
    WHERE post_type = 'post' AND post_status = 'publish'
    GROUP BY DATE_FORMAT(post_date_gmt, '%Y')
    ORDER BY post_date_gmt DESC");
    
    if ( $years && count($years) > 0 ) {
    	foreach ( $years as $y ) { ?>
            <div class="wrap">
                <div class="year"><?php echo $y; ?></div><?php 
    
        		$months =  $wpdb->get_col("SELECT DATE_FORMAT(post_date_gmt, '%m')
    FROM $wpdb->posts
    WHERE post_type = 'post' AND post_status = 'publish'
    AND DATE_FORMAT(post_date_gmt, '%Y') = $y
    GROUP BY DATE_FORMAT(post_date_gmt, '%Y-%m')
    ORDER BY post_date_gmt DESC");
    
    			if ($months) { ?>
                    <ul><?php
    				foreach($months as $m) {
    					echo '<li><a href="'.get_month_link($y, $m).'">'.$month[zeroise($m, 2)]."</a></li>";
    				} ?>
                    </ul><?php
    			} ?>
            </div><?php
    	}
    
    } ?>

    Hello, you should use getarchives_where filter.

    I have create this callback function:

    function takien_archive_where($where,$args){
    	$year		= isset($args['year']) 		? $args['year'] 	: '';
    	$month		= isset($args['month']) 	? $args['month'] 	: '';
    	$monthname	= isset($args['monthname']) ? $args['monthname']: '';
    	$day		= isset($args['day']) 		? $args['day'] 		: '';
    	$dayname	= isset($args['dayname']) 	? $args['dayname'] 	: '';
    
    	if($year){
    		$where .= " AND YEAR(post_date) = '$year' ";
    		$where .= $month ? " AND MONTH(post_date) = '$month' " : '';
    		$where .= $day ? " AND DAY(post_date) = '$day' " : '';
    	}
    	if($month){
    		$where .= " AND MONTH(post_date) = '$month' ";
    		$where .= $day ? " AND DAY(post_date) = '$day' " : '';
    	}
    	if($monthname){
    		$where .= " AND MONTHNAME(post_date) = '$monthname' ";
    		$where .= $day ? " AND DAY(post_date) = '$day' " : '';
    	}
    	if($day){
    		$where .= " AND DAY(post_date) = '$day' ";
    	}
    	if($dayname){
    		$where .= " AND DAYNAME(post_date) = '$dayname' ";
    	}
    	return $where;
    }

    Usage and example can be found here:
    http://takien.com/1092/display-wordpress-archive-lists-by-specific-year-or-month.php

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using wp_get_archives to filter by year?’ is closed to new replies.
Skip to toolbar