Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • WordPress Loop – Include / Exclude Tags Globally

WordPress Loop – Include / Exclude Tags Globally

0

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' );
PHP, Tutorial, WordPress

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bash Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Hacking HTML5 Input Sanitization iOS JavaScript jQuery Laravel 5 Laravel 5.2 Linux Mac OS NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy