• Resolved Serena Zehlius

    (@thezanyprogressive)


    Is there a way to make archived posts still show up in the category and tag archives? I ask because a lot of my tags now show as having 0 posts even though there are a lot of posts with that tag, they are just archived? Google Adsense brought to my attention the number of empty tag pages on my site. For instance 2020 election has a lot of posts in it, but shows as having zero because the posts are archived. I don’t want archived posts showing on the front page, but I want people to be able to find them in search results and on archive pages; taxonomy and date. Is this possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author shawfactor

    (@shawfactor)

    Something like this should work:

    
    function exclude_archive_post_status_from_main_query( $query ) {
    	
    	    if ( $query->is_main_query() && !is_admin() && !is_singular() && empty($_GET['s']) && !is_tag() && !is_category()) {
    
                if ( current_user_can('read_private_posts') ) {
    
                    $post_status = array( 'publish', 'private' );
    
                } else {
    
                    $post_status = array( 'publish');
    
                }
                
    		    $query->set( 'post_status', $post_status );
    		
    	    }
    	    
        }
    function exclude_archive_post_status_from_main_query( $query ) {
    	
    	    if ( $query->is_main_query() && !is_admin() && !is_singular() && empty($_GET['s']) && !is_tag() && !is_category()) {
    
                if ( current_user_can('read_private_posts') ) {
    
                    $post_status = array( 'publish', 'private' );
    
                } else {
    
                    $post_status = array( 'publish');
    
                }
                
    		    $query->set( 'post_status', $post_status );
    		
    	    }
    	    
        }
    //Handle access and display of the archived post status
            add_action( 'pre_get_posts', 'exclude_archive_post_status_from_main_query', 15, 1);

    Paste in functions or site specific plugin, note I’ve not tested it but should work

    Thread Starter Serena Zehlius

    (@thezanyprogressive)

    ok, cool! I’ll give it a try. I’ll be pasting it into WP-code snippets. Should it run in the header? And is this php? (Yes, I’m that dumb when it comes to coding.)

    Plugin Author shawfactor

    (@shawfactor)

    it’s php, I’d put it in functions.php in your theme or a site specific plugin.

    please try it on a test environment first, as I’ve not tested it myself.

    • This reply was modified 1 year, 2 months ago by shawfactor.
    Plugin Author shawfactor

    (@shawfactor)

    I am resolving the thread for good order but will reopen this thread if there is further activity

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Still show posts in category and tag archives?’ is closed to new replies.