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 from Template Files

WordPress Loop – Include / Exclude Tags from Template Files

1

If you want to filter certain posts by tags in your WordPress theme, here is a simple way to do so before executing the WordPress loop. This method lets you exclude posts that contain certain tags, or you can easily change the code to include post with certain tags instead.

Solution

//	Modify the query
$query_string['tag__not_in'] = array(1,4,5);
query_posts( $query_string );

//	WordPress Loop
if (have_posts()) :
	while (have_posts()) : the_post(); update_post_caches($posts);
		//	Display posts here
	endwhile;
else :
	//	No posts found
endif;

Modification

If you want to include certain tag IDs, you only need to change one line of the code.

Change:

$query_string['tag__not_in'] = array(1,4,5);

To:

$query_string['tag__in'] = array(1,4,5);
Tutorial, WordPress

One comment on “WordPress Loop – Include / Exclude Tags from Template Files”

  1. Alan says:
    May 23, 2013 at 5:33 PM

    That’s what i was looking for! Thanks!

    Reply

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 Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 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