• Resolved Pioneer Web Design

    (@swansonphotos)


    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( 'https://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?

Viewing 1 replies (of 1 total)
  • Thread Starter Pioneer Web Design

    (@swansonphotos)

    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:

    https://www.ads-software.com/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 !!

Viewing 1 replies (of 1 total)
  • The topic ‘Archive Page(s) & Redirect Issue’ is closed to new replies.