WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Archive Page(s) & Redirect Issue (2 posts)

  1. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    At this site, I am using below in functions.php of 2011 Child Theme to redirect non logged in users to the home page when they attempt to access the category (which works initially as intended):

    add_action('template_redirect','cat_redirect');function cat_redirect(){ if ((in_category('memories')) && !is_user_logged_in() ) { wp_redirect( 'http://swansonheritage.com/' ); die(); }}

    What I just noted is that if an archive page includes a post in that category, it will also re-direct to the home page...for example here

    Any ideas on a best solution?

  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    Finally got back to this...is resolved now using:

    define("EXCLUDED_CATEGORIES", '25,55,57');
    
    add_filter( 'getarchives_join' , 'getarchives_join_filter');
    function getarchives_join_filter( $join ) {
    	global $wpdb;
    	return $join . " INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id) INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
    }
    
    add_filter( 'getarchives_where' , 'getarchives_where_filter');
    function getarchives_where_filter( $where ) {
    	global $wpdb;
    
    	$exclude = EXCLUDED_CATEGORIES; // category ids to exclude
    	return $where . " AND tt.taxonomy = 'category' AND tt.term_id NOT IN ($exclude)";
    
    	}
    
    // exclude categories on monthly archive pages
    function my_post_queries( $query ) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for monthly archive pages
    		if(is_archive() && is_month()){
    			$query->set('category__not_in', array(EXCLUDED_CATEGORIES));
    		}
    	}
    }
    
    add_action( 'pre_get_posts', 'my_post_queries' );

    Per this post:

    http://wordpress.org/support/topic/remove-cetain-post-categories-from-blog-sidebar-archive-list?replies=10

    I obviously updated which category to exclude from archive pages.

    Special thanks to kessiemeijor !!

Topic Closed

This topic has been closed to new replies.

About this Topic