Forums

Archives Function: Months -> Years and category exclusion (1 post)

  1. Actarus
    Member
    Posted 10 months ago #

    Hello,

    I have 2 problems with a function I found :-(

    Here is the function:

    function wp_get_archives_advanced($args = '') {
    	global $wpdb, $wp_locale;
    
    	$defaults = array(
    		'pivot' => 0,
    		'limit' => '',
    		'format' => 'html',
    		'before' => '',
    		'after' => '',
    		'show_post_count' => false
    	);
    	$defaults['pivot'] = date('Y');
    
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if ( '' != $limit ) {
    		$limit = (int) $limit;
    		$limit = ' LIMIT '.$limit;
    	}
    
    	$arcresults = $wpdb->get_results("SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) AS 'posts' FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
    	$pivot_year = date('Y');
    	$current_year= '';
    	if ( $arcresults ) {
    		$afterafter = $after;
    		foreach ( $arcresults as $arcresult ) {
    			if ($current_year == '') $current_year = $arcresult->year;
    			if ($arcresult->year < $pivot_year) {
    				if ($current_year <> $arcresult->year) {
    
    					$url  = get_year_link($arcresult->year);
    					$text = sprintf(__('%1$d'), $arcresult->year);
    					if ( $show_post_count )
    						//$after = ' (<span class="red">'.$arcresult->posts.'</span>)' . $afterafter;
    					echo get_archives_link($url, $text, $format, $before);
    				}
    			} else {
    				$url  = get_month_link($arcresult->year, $arcresult->month);
    				$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    				if ( $show_post_count )
    					$after = ' (<span class="red">'.$arcresult->posts.'</span>)' . $afterafter;
    				echo get_archives_link($url, $text, $format, $before, $after);
    			}
    			$current_year = $arcresult->year;
    		}
    	}
    }

    This function create something like this:

    Jully 2011 (25)
    June 2011 (8)
    May 2011 (34)
    etc...
    2010 (150) -> "Count" isn't working properly
    2009 (225) -> "Count" isn't working properly

    It works quite perfectly but when the function runs for the past years the "count" isn't working properly there is a problem with the SQL request, I tried but I was unable to solve that :-(

    And my second problem is that I would like to exclude the category number "5" in the archives with the above function... I think it's possible to with the SQL request but it's becoming too tricky for me.

    Any help would be really appreciated, thank you.

    Best regards.

Reply

You must log in to post.

About this Topic