WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_archives offset (2 posts)

  1. thecoup
    Member
    Posted 5 years ago #

    It'd be great if wp_get_archives supported offset in 2.7

    thanks for all your great work...

  2. medecau
    Member
    Posted 5 years ago #

    oh hay!

    I had the same problem today while retouching my blog.
    Had to change the original function, here goes the changed version.
    Be careful has this may have bugs. And post back if everything goes well.

    Here goes:

    function wp_get_archives($args = '') {
    	global $wpdb, $wp_locale;
    
    	// ADD NICE DEFAULT VALUE PLEASE!
    	$defaults = array(
    		'type' => 'monthly', 'limit' => '',
    		'format' => 'html', 'before' => '',
    		'after' => '', 'show_post_count' => false,
    		'echo' => 1, 'offset' => 0
    	);
    
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if ( '' == $type )
    		$type = 'monthly';
    
    	if ( '' != $limit ) {
    		$limit = absint($limit);
    		$limit = ' LIMIT '.$limit;
    	}
    
    	// CONTROL COPY AND PASTE, fix names.
    	if ( '' != $offset ) {
    		$offset = absint($offset);
    		$offset = ' OFFSET '.$offset;
    	}
    
    	// this is what will separate dates on weekly archive links
    	$archive_week_separator = '–';
    
    	// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
    	$archive_date_format_over_ride = 0;
    
    	// options for daily archive (only if you over-ride the general date format)
    	$archive_day_date_format = 'Y/m/d';
    
    	// options for weekly archive (only if you over-ride the general date format)
    	$archive_week_start_date_format = 'Y/m/d';
    	$archive_week_end_date_format	= 'Y/m/d';
    
    	if ( !$archive_date_format_over_ride ) {
    		$archive_day_date_format = get_option('date_format');
    		$archive_week_start_date_format = get_option('date_format');
    		$archive_week_end_date_format = get_option('date_format');
    	}
    
    	//filters
    	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    	$join = apply_filters('getarchives_join', "", $r);
    
    	$output = '';
    	// ADD NICE $offset TO THE END, THX!
    	if ( 'monthly' == $type ) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit $offset";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			$afterafter = $after;
    			foreach ( (array) $arcresults as $arcresult ) {
    				$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 = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    				$output .= get_archives_link($url, $text, $format, $before, $after);
    			}
    		}
    			// ADD NICE $offset TO THE END AGAIN, THX!
    	} elseif ('yearly' == $type) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit $offset";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ($arcresults) {
    			$afterafter = $after;
    			foreach ( (array) $arcresults as $arcresult) {
    				$url = get_year_link($arcresult->year);
    				$text = sprintf('%d', $arcresult->year);
    				if ($show_post_count)
    					$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    				$output .= get_archives_link($url, $text, $format, $before, $after);
    			}
    		}
    			// ADD NICE $offset TO THE END AND AGAIN, THX!
    	} elseif ( 'daily' == $type ) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, DAYOFMONTH(post_date) AS <code>dayofmonth</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit $offset";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			$afterafter = $after;
    			foreach ( (array) $arcresults as $arcresult ) {
    				$url	= get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
    				$date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
    				$text = mysql2date($archive_day_date_format, $date);
    				if ($show_post_count)
    					$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    				$output .= get_archives_link($url, $text, $format, $before, $after);
    			}
    		}
    			// ADD NICE $offset TO THE END ONCE MORE, THX!
    	} elseif ( 'weekly' == $type ) {
    		$start_of_week = get_option('start_of_week');
    		$query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS <code>week</code>, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit $offset";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		$arc_w_last = '';
    		$afterafter = $after;
    		if ( $arcresults ) {
    				foreach ( (array) $arcresults as $arcresult ) {
    					if ( $arcresult->week != $arc_w_last ) {
    						$arc_year = $arcresult->yr;
    						$arc_w_last = $arcresult->week;
    						$arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
    						$arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
    						$arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
    						$url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', get_option('home'), '', '?', '=', $arc_year, '&amp;', '=', $arcresult->week);
    						$text = $arc_week_start . $archive_week_separator . $arc_week_end;
    						if ($show_post_count)
    							$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    						$output .= get_archives_link($url, $text, $format, $before, $after);
    					}
    				}
    		}
    	} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    		$orderby = ('alpha' == $type) ? "post_title ASC " : "post_date DESC ";
    		$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			foreach ( (array) $arcresults as $arcresult ) {
    				if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
    					$url  = get_permalink($arcresult);
    					$arc_title = $arcresult->post_title;
    					if ( $arc_title )
    						$text = strip_tags(apply_filters('the_title', $arc_title));
    					else
    						$text = $arcresult->ID;
    					$output .= get_archives_link($url, $text, $format, $before, $after);
    				}
    			}
    		}
    	}
    	if ( $echo )
    		echo $output;
    	else
    		return $output;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic