InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress customization tutorials

  • Handle pagination in wordpress custom php code
  • How to and why disable wordpress cron
  • How to customize wordpress image alt tag
  • How to display date in wordpress pages
  • How to display wordpress page list with specific custom field value
  • How to display wordpress top level pages
  • How to hide a post from home and RSS feed in wordpress
  • How to include bootstrap javascript and css in wordpress post
  • How to include wordpress pages in archives
  • How to override priority and change frequency in Yoast xml sitemap
  • How to remove xmlrpc from wordpress headers
  • How to setup wordpress custom query params with pretty url format
  • How to show wordpress pages on front page with skip_home custom field
  • How to use google custom search for wordpress site
  • How to write custom php in wordpress
  • No frills social share links for Wordpress sites
  • Using Wordpress custom field for displaying code
  • Wordpress - add content filter after shortcode execution
  • Wordpress - customize category and tag links
  • Wordpress - customize facebook plugin opengraph meta tags
  • Wordpress - customize posts per page for tag, category and date archive pages
  • Wordpress - exclude specific posts from archive pages
  • Wordpress - get posts/pages with missing meta key
  • Wordpress - how to add filter to description meta tag
  • Wordpress - how to create custom tag cloud
  • Wordpress - how to exclude specific tag posts from a tag archive page
  • Wordpress - write custom php log to separate file
  • Wordpress how to check if a post is being viewed by admin
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress customization

WordPress – how to exclude specific tag posts from a tag archive page

By admin on Jan 16, 2016

WordPress will display all posts on a tag archive page which are tagged with that tag. Sometimes we want to exclude posts of more specific tags to reduce number of pages. Here is the code snippet example which we can refer to for this.

add_action('pre_get_posts', 'pre_get_posts_tag_exclude_some_tags');
function pre_get_posts_tag_exclude_some_tags ($query) {
  if (!is_tag() || !$query->get('tag')) {
    return;
  }
  $tagobj = get_term_by('slug', $query->get('tag'), 'post_tag');
  $tagslugs_to_exclude = array('foo', 'bar');
  $tagids_to_exclude = array();
  foreach ($tagslugs_to_exclude as $oneslug) {
    $onetagobj = get_term_by('slug', $oneslug, 'post_tag');
    $tagids_to_exclude[] = $onetagobj->term_id;
  }
  $query->set('tag__not_in', $tagids_to_exclude);
}

Steps involved

  1. We added an action on WordPress hook pre_get_posts to modify the wordpress query before it gets executed.
  2. In action code we ensured that we are on tag page (is_tag()) and query should be for displaying posts for the tag page ($query->get('tag')). This is to avoid modifying query for other wordpress queries (e.g. getting menu list, etc.)
  3. We used tag__not_in param in query object (type QP_Query) to ignore specific tags.

Note that this code may reduce your total number of pages for a tag archive page. But posts per page will remain same.

Suggested posts:

  1. find if a wordpress page is leaf page
  2. Fetch wordpress rss feed as FeedBurner user agent on command line
  3. Svn – how to edit log message for a committed change
  4. How to setup MailChimp Rss email campaign
  5. HTML li tag
  6. How to install AWStats on Ubuntu Linux
  7. How to add tag and category to wordpress pages
  8. How to display wordpress top level pages
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Tutorials, Wordpress, Wordpress customization
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress