Site logo

Archived topic

Exclude Posts With Specific Tag From Blog Archive Page

12 replies · Started by Heather on October 28, 2019

Viewing posts 1–13 of 13

Hi there,

How can I exclude posts that have a specific tag from the main blog archive page? Ie: If I have a tag of 'happy' I add to some posts, I do not want any posts with the 'happy' tag to show up in the blog archive/"posts" page.

I used this code to exclude the tag by tag ID but it didn't work.


function exclude_tag( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
	$query->set( 'tag', '179' );
	}
	}
	add_action( 'pre_get_posts', 'exclude_tag' );

I also tried it with a '-' sign in front of the id, so '-179' and that did not work either

Hi there,

Can you try this?

add_filter( 'pre_get_posts', function( $query ) {
    if ( $query->is_home() && $query->is_main_query() )
        $query->set( 'tag', '-19' );
    }

    return $query;
} );

The return $query gives this error:

If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

If I get rid of it, my blog archive page just says "ready for your first post?", so it seems to eliminate all posts.

Is there a way to target it via slug? I have "blog" set to my "post page" in my Reading settings.

Try this:

function exclude_tag_home( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$query->set( 'tag', '-19' );
	}
	return $query;
}
add_filter( 'pre_get_posts', 'exclude_tag_home' );

Unfortunately that does the same thing where it eliminates all posts from the blog archive. Is the blog archive dictated by index.php?

Is there a way to target the index.php file or the "posts" page? Maybe that code is targeting that page I'm not sure, I just see home so I keep thinking its the home page.

Thanks

Yes, that worked! Thank you!

You're welcome :)

Hi, I realized I needed something else with this -

So I have 6 categories and 4 tags to differentiate posts (webinar, case-study, etc). I noticed that, although these posts are now hidden on the blog archive page, I also need them hidden within the archive category page as well.

I have specific pages to show the tag archive for these, and specific urls for categories within those tag archives to filter by cat + tag. So, a breakdown of URL's:

/blog (blog archive/posts page)

/blog/category/devops (devops category archive page <- currently displays everything with that category regardless of tag)

/blog/tag/webinar (webinar tag archive page <- displays everything with the tag of webinar)

/blog/category/devops/?tag=webinar (an archive page that displays only posts with both the category of devops and tag of webinar)

I am using the code below to exclude webinar and case-studies from the blog archive page, but what would I add to this to ensure that posts with these tags are excluded on the category archive pages, but will still appear in the urls of the last two shown - tag archive and tag+cat (devops + webinar). Is this possible?

	function exclude_posts( $query ) { 

		if ( $query->is_home() ) {  
	
			$query->set( 'tag__not_in', array( 182, 180, 166 ) ); 
	
		}
	} 
	
	add_action( 'pre_get_posts', 'exclude_posts' );

Hmm that sounds quite complicated and I'm not sure if it's possible.

Have you tried asking on a forum like Stackoverflow?
https://wordpress.stackexchange.com/

This isn't really a theme question.

Ok, I guess the better question is how can I change this to only show posts of a specific tag. Would I just change the tag__not_in to tag_in?

This archived topic is closed to new replies.