Quick n’ Dirty Post ExclusionFiled Under: Snippets, plugins

This is the third of my Quick N Dirty plugin posts. In the second post I showed how to access the post object and alter the post content before saving it back to the database. This plugin will also access to the post object, but this time I will use it to exclude posts from being displayed on the home page.
The hook I am going to use for this plugin is the_posts filter. This filter is called when a list of posts is pulled from the WordPress database ready to be displayed on screen.
The filter passes an array of post objects to the function which gives us the opportunity to carry out mass edits, add posts, or, as in this case, remove posts in certain circumstances.
What I want to do is to make sure that posts in my General category do not appear on the home page. I want them as part of my feed, as an added sweetener for subscribing, and I want them as part of my single post and my archives.
Here’s the plugin, I will explain more at the bottom:
<?php /* Plugin Name: Quick N Dirty Post Exclusion Plugin URI: [insert the plugin uri here] Description: Removes posts from the home page Author: Andrew Rickmann Version: 1 Author URI: http://www.wp-fun.co.uk */ //call the filter //the_posts is called when a list of posts is retrieved from the database //qnd_post_exclusion is the function //1 is the priority, 1 runs first, 10 last add_filter('the_posts', 'qnd_post_exclusion' , 1 ); function qnd_post_exclusion( $posts ){ //only apply this if we are looking at the home page if ( !is_home() ) { return $posts; } //create an array to hold the posts we want to show $new_posts = array(); //loop through all the post objects foreach( $posts as $post ){ //for each object get an array of applicable categories $cats = get_the_category( $post->ID ); //create a variable that determins if this post should be included //the default is true, i.e. include the post $include = true; //loop through all the categories applicable to the post foreach( $cats as $cat) { //if the post is assigned to our undesirable category then do not include it if ( $cat->name == 'General' ) { $include = false; } } //if we want to include it then add the post object to the new posts array if ( $include === true ) { $new_posts[] = $post; } } //send the new post array back to be used by WordPress return $new_posts; } ?>
By now the add_filter function should be fairly familiar so I won’t dwell on it. It shows the the hook, the function to be used, and specifies that it should run with the highest priority. I have chosen high priority so that any plugins that follow on that might modify the content of the post are saved the trouble of modifying a post that will later be removed. Aside from that the priority isn’t too relevant here.
The usual template functions is_home, is_feed, is_archive, etc will work at this point and so I have used is_home to restrict the plugin to posts displayed on the home page. If we are not dealing with the home page then the function will simply return the original array of posts. It is essential that it does return the array. Not returning the array is the same as filtering out every post, leaving no posts at all.
When the function is called it is passed an array of post objects. We use the post ID in each case to get an array of the categories the post is assigned to using the function below:
get_the_category( $post->ID );
We then iterate through this array and if our chosen category is not in the array for each post we add the post to a new array . When we are done it is this array that is passed back to WordPress to continue its journey toward screen.
Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.
- Permalink
- Andrew Rickmann
- 26 Jan 2008 3:14 PM
- Comments (0)