After my post about filtering certain posts by tags, I wondered how you could hide / show posts globally across WordPress. For example, I wanted to show all the posts tagged as ‘PlayStation Vita’ on my XTREME PSVita website. Why? The XTREME PSVita website uses the same database as XTREME PSP, but I didn’t want the posts to appear in both areas at once.
I created a simple plugin that only showed tagged posts on one website, and not the other. This plugin works on a global level, so even posts in the WordPress Admin area are filtered, and it will continue to work if you change or update your theme too.
Solution
// function to filter tags function filter_xtreme_tags( $query ) { // determine which website we are on if ( isset( $_SERVER['HTTP_HOST'] ) && strstr( $_SERVER['HTTP_HOST'], 'xtremepsvita.com' ) ) { // are we looking at a post? this bit is important if ( $query->is_post ) { // only show posts tagged as 'PlayStation Vita' $query->set( 'tag__in', array(163) ); } } else { // we are on xtremepsp.com // don't show posts tagged as 'not-xtremepsp' $query->set( 'tag__not_in', array(200) ); } } // add the filter to wordpress add_action( 'pre_get_posts', 'filter_xtreme_tags' );
Modification
The tag__in
an tag__not_in
parameters only accept arrays, so you can easily include or exclude multiple tags from your blog.
If you want to filter by categories instead, replace the $query-set part of the code to something like:
// only show posts from cat 3 $query->set( 'cat', '3' ); // hide posts from cat 3 $query->set( 'cat', '-5' );