Created
February 5, 2011 15:57
-
-
Save hakre/812542 to your computer and use it in GitHub Desktop.
WordPress 3.1 Taxonomy Feeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WordPress 3.1 Taxonomy Feeds Plugin (Must-Use) | |
* | |
* Copyright (C) 2011 hakre <http://hakre.wordpress.com/> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Affero General Public License as | |
* published by the Free Software Foundation, either version 3 of the | |
* License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Affero General Public License for more details. | |
* | |
* You should have received a copy of the GNU Affero General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* USAGE: | |
* | |
* http://example.net/feed?taxonomy=people | |
* | |
* @author hakre <http://hakre.wordpress.com> | |
* @see http://wordpress.stackexchange.com/questions/3720/rss-feed-for-posts-containing-any-term-from-a-taxonomy | |
*/ | |
/* feel free to uncomment / remove the next hook as it's only in here for demo */ | |
add_filter('init', function() { | |
# register demo "people" taxonomy | |
register_taxonomy( | |
'people', | |
'post', | |
array( | |
'label' => __('People'), | |
'sort' => true, | |
'args' => array('orderby' => 'term_order'), | |
'rewrite' => array('slug' => 'person') | |
) | |
); | |
}); /* end of demo hook */ | |
add_filter('pre_get_posts', function(WP_query $query) { | |
if (!($taxonomyName = $query->get('taxonomy')) && !$query->is_feed) | |
return; # quick return if not a feed and parameter missing. | |
$taxonomyIds = get_terms($taxonomyName, array('fields' => 'ids')); | |
if (is_wp_error($taxonomyIds)) | |
return; # quick return if taxonomy does not exists | |
$taxonomyQuery = array( | |
'taxonomy' => $taxonomyName, | |
'terms' => $taxonomyIds, | |
); | |
isset($query->query_vars['tax_query']) | |
|| $query->query_vars['tax_query'] = array() | |
; | |
$query->query_vars['tax_query'][] = $taxonomyQuery; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment