WordPress.org

Ready to get started?Download WordPress

Forums

Remove Cetain Post Categories From Blog Sidebar Archive List (10 posts)

  1. mattybrown1
    Member
    Posted 1 year ago #

    I have excluded certain categories of posts from my blog so they never appear on my blog. However, in the blog chronological archive list that appears in the right side bar, the 6 excluded posts still appear as links and clicking on the link takes you to a list of the excluded posts.

    Can any one advise on how to remove these posts from the chronological archive?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you share a link to your site? What theme are you using?

  3. mattybrown1
    Member
    Posted 1 year ago #

    It is ithemes builder with classen child theme. I have posted on their forum but I am not having much luck.

    Warning - The site belongs to a professional author who writes erotic fiction of the Fifty Shades of Grey genre! http://www.tessavalmur.com/blog/

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try excluding the categories with this in your theme's functions.php:

    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' );

    Change the category IDs you want to exclude in define("EXCLUDED_CATEGORIES", '25,55,57'); to the correct category ID's

  5. mattybrown1
    Member
    Posted 1 year ago #

    Thank you - that works well. The unwanted post categories have gone from the chronological archive list. But it has thrown up another problem which I'm not sure if is related.

    When you click on the October archives, a list of posts is displayed (there are currently two - post 1 and post 2) but only the body text of the post displays - no headings or details.

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Did headings or details show up before you used my code?

    Can you paste and submit the full code of archive.php of your theme into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

  7. mattybrown1
    Member
    Posted 1 year ago #

    I have just tried removing your code from the function.php and the same problem was still there for the 'blog' category posts (posts 1 and 2) but the 'novels' category posts were formatted as per the actual Novels page. (I have now replaced your code).

    Here is the code for the archive.php

    http://pastebin.com/PTQ5zmSa

    Thanks for you help.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    This in your stylesheet style.css is hiding the titles:

    /*********************************************
        post/page titles
    *********************************************/
    .entry-title {
     display: none;
    }

    Remove it and the titles will show up.

  9. mattybrown1
    Member
    Posted 1 year ago #

    Many thanks for you help.

  10. AntonioFajdiga
    Member
    Posted 1 year ago #

    i also tried

    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' );

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    but this exclude only the first cat_id, for example if i have '1,3,8' it will exclude only cat_id=1. How i can fix this?

Topic Closed

This topic has been closed to new replies.

About this Topic