WordPress.org

Ready to get started?Download WordPress

Forums

Exclude categories from custom blog archive page (1 post)

  1. conceptify
    Member
    Posted 5 years ago #

    Ok I am working on a site that has an existing custom Archive page that was done by a freelancer. It's a bit too complicated for me to make the edit that I need to make.

    Basically the client is using certain categories for different content that they don't want to show up on the blog Archive page.

    So I think I need to edit the query to the database to exclude the certain categories:

    if ( ! empty( $category__in ) ) {
    		$join = " 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) ";
    		$where .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
    		$include_cats = "'" . implode("', '", $category__in) . "'";
    		$where .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
    	} else {
    		$join = apply_filters('getarchives_join', "", $r);
    	}

    Any help would be much appreciated!

    Here is the complete function in functions.php :

    function custom_archives($args = null) {
    	global $wpdb, $wp_locale;
    
    	$defaults = array(
    		'type' => 'monthly', 'limit' => '', 'category__in' => array(),
    		'format' => 'html', 'before' => '',
    		'after' => '', 'show_post_count' => false, 'show_posts' => true
    	);
    
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if ( '' == $type )
    		$type = 'monthly';
    
    	if ( '' != $limit ) {
    		$limit = (int) $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');
    	}
    
    	$add_hours = intval(get_option('gmt_offset'));
    	$add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
    
    	//filters
    	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    	if ( ! empty( $category__in ) ) {
    		$join = " 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) ";
    		$where .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
    		$include_cats = "'" . implode("', '", $category__in) . "'";
    		$where .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
    	} else {
    		$join = apply_filters('getarchives_join', "", $r);
    	}
    
    	if ( 'monthly' == $type ) {
    		$arcresults = $wpdb->get_results("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);
    		if ( $arcresults ) {
    			$afterafter = $after;
    			foreach ( $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);
    				/* Query posts for that month */
    				$after = '';
    				if ( $show_posts ) :
    					query_posts(array('monthnum' => $arcresult->month, 'year' => $arcresult->year));
    					while (have_posts()) : the_post();
    						$after .= '<div class="post">
    						<h3><a href="' . get_permalink() . '" rel="bookmark" title="Continue reading ' . get_the_title() . '">' . get_the_title() . '</a></h3>
    
    						<div class="blog-post">
    
    							<h4>' . get_the_time('l, F jS, Y') . ' at ' . get_the_time('g:i A') . ' EST ' . '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>' . '</h4>
    
    							' . get_the_excerpt("Continue reading...") . '
    
    						</div>
    					</div>';
    					endwhile;
    				endif;
    				if ( $show_post_count )
    					$text = $text . '&nbsp;('.$arcresult->posts.')';
    				if ( $show_posts )
    					echo get_archives_link($url, $text, 'html', '<h2>', '</h2>' . $after);
    				else
    					echo get_archives_link($url, $text, 'html', '', '' . $after);
    			}
    		}
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic