WordPress.org

Ready to get started?Download WordPress

Ideas

Idea: improved wp_get_archives

  1. milardovich
    Member

    12345

    Howdy!,
    Today i was just working with a wordpress theme and i must make an archive with the post of an specific category, talking with some floks in the irc and searching in the docs i found the wp_get_archives function, but it hadn't the option of select specific categories in the argument, so, i just added it.
    The code:

    <code>
    function wp_get_archives($args = '') {
    	global $wpdb, $wp_locale;
    
    	$defaults = array(
    		'type' => 'monthly', 'limit' => '',
    		'format' => 'html', 'before' => '',
    		'after' => '', 'show_post_count' => false,
    		'echo' => 1, 'cat' => -1
    	);
    
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if($cat !== -1){
    		$cats = explode(",",$cat);
    		$extended_where;
    		$i=1;
    		$count = count($cats);
    		foreach($cats as $c){
    			if($i == 1 && $count>1){
    				$extended_where .= 'AND term_taxonomy_id = '.$c.' ';
    			} else {
    				$extended_where .= "OR post_type = 'post' AND post_status = 'publish' AND object_id = ID AND term_taxonomy_id = $c ";
    			}
    			$i++;
    		}
    	} else {
    	  $cat_query = NULL;
    	}
    
    	if ( '' == $type )
    		$type = 'monthly';
    
    	if ( '' != $limit ) {
    		$limit = absint($limit);
    		$limit = ' LIMIT '.$limit;
    	}
    
    	// 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
    	if(!$extended_where){
    	  $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish' AND object_id = ID", $r );
    	} else {
    	  $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish' AND object_id = ID $extended_where", $r );
    	}
    
    	$join = apply_filters('getarchives_join', "", $r);
    
    	$output = '';
    
    	if ( 'monthly' == $type ) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code></code>year<code>, MONTH(post_date) AS</code>month
    , count(ID) as posts FROM $wpdb->posts, $wpdb->term_relationships $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $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 ) {
    $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 = ' ('.$arcresult->posts.')' . $afterafter;
    $output .= get_archives_link($url.'&c='.$cat, $text, $format, $before, $after);
    }
    }
    } elseif ('yearly' == $type) {
    $query = "SELECT DISTINCT YEAR(post_date) AS year, count(ID) as posts FROM $wpdb->posts, $wpdb->term_relationships $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $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) {
    $afterafter = $after;
    foreach ( (array) $arcresults as $arcresult) {
    $url = get_year_link($arcresult->year);
    $text = sprintf('%d', $arcresult->year);
    if ($show_post_count)
    $after = ' ('.$arcresult->posts.')' . $afterafter;
    $output .= get_archives_link($url.'&c='.$cat, $text, $format, $before, $after);
    }
    }
    } elseif ( 'daily' == $type ) {
    $query = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, DAYOFMONTH(post_date) AS dayofmonth, count(ID) as posts FROM $wpdb->posts, $wpdb->term_relationships $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $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 ) {
    $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 = ' ('.$arcresult->posts.')'.$afterafter;
    $output .= get_archives_link($url.'&c='.$cat, $text, $format, $before, $after);
    }
    }
    } elseif ( 'weekly' == $type ) {
    $start_of_week = get_option('start_of_week');
    $query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS week, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts, $wpdb->term_relationships $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $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 ];
    }
    $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, '&', '=', $arcresult->week);
    $text = $arc_week_start . $archive_week_separator . $arc_week_end;
    if ($show_post_count)
    $after = ' ('.$arcresult->posts.')'.$afterafter;
    $output .= get_archives_link($url.'&c='.$cat, $text, $format, $before, $after);
    }
    }
    }
    } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    $orderby = ('alpha' == $type) ? "post_title ASC " : "post_date DESC ";
    $query = "SELECT * FROM $wpdb->posts $join, $wpdb->term_relationships $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.'&c='.$cat, $text, $format, $before, $after);
    }
    }
    }
    }
    if ( $echo )
    echo $output;
    else
    return $output;
    }

    Sorry for my bad english, hope you understand me, if somebody of the development guys read it, please, add the code(or the idea) to the next wordpress release ;-)
    Btw, you can also add a new arg for authors or ids,
    Cheers,
    Milardovich

    Posted: 5 years ago #
  2. owlman
    Member

    12345

    Dear Milardovich,

    It is not clear how to use this code the code itself contain to much "Code" tags in wrong place.

    Could you please clarify usage of your input to beginners.

    Posted: 4 years ago #
  3. mario oyunu
    Member

    It is not clear how to use this code the code itself contain to much "Code" tags in wrong place.

    Posted: 4 years ago #
  4. Robert Chapin
    Member

    12345

    Sounds like a nice plugin. Essentially OP is asking for an Archives widget that can be context sensitive to category views. That idea would have to be extended to tags, taxa, and custom post types before going into core. I doubt there's enough demand for that now.

    Posted: 4 years ago #
  5. rumlab
    Member

    12345

    From searching the forums, and the 188 replies on kwebble site, there definately seems to be a demand.

    I think these are core functions to use wordpress as a flexible CMS.

    Posted: 4 years ago #
  6. cricxdf
    Member

    @milardovich This is very good code for the archeive and give another look to my blog on wordpress and This make this Cms and one of the most good CMS And Wp_Get_archeives Function is to be used in the Start on the Archeives

    Posted: 2 years ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    9 Votes
  • Status

    This is plugin territory