This starts when I found what seemed like just the right WordPress event calendar plugin, all-in-one Event-calendar, with lots of good features, useful shortcodes etc. However, like many event plugins, it uses a custom post type, and normally will not show up in blog postings. My client wants scheduled events to show up in the blog home, and lists by category or tag as well. Say, clicking “Live Music” should bring up videos and photo albums of past performances and scheduled future event info. Finding out how to add the custom post type was not too hard, but the events use their own custom “taxonomy” as well, their own kind of category and tag that aren’t really the category and tag of regular posts. I really wanted to solve this problem, not just for this specific plugin, but for any custom post types and taxonomies I run into in the future as well. This has led me through some murky sketchily documented regions of the WordPress world around custom taxonomies and query modification, to at last arrive at this piece of code that can be placed in any theme’s functions.php file, and will cause ai1ec_event type custom posts to be displayed in blog home, feeds, and lists by category or tag. This uses the very handy pre_get_posts action hook which can change which posts are displayed without having to change anything else about the blog, thus keeping the code nice and portable. This can be modified to query across any set of custom post types or taxonomies.
// Add this to your theme's functions.php
function edit_my_query($query) {
// Modify category and tag listings to include ai1ec events and all uses of the same term
// across event and post taxonomies
// ie live-music or arts whether they are event or post categories
// also include ai1ec events in blog home and feeds
if ( ( is_home() || is_feed() || is_category() || is_tag() )
&& empty( $query->query_vars['suppress_filters'] ) ) {
// The 'suppress_filters' test above keeps your menus from breaking
$post_type = get_query_var('post_type');
if($post_type && $post_type[0] != 'post') {
$post_type = $post_type;
} else {
$post_type = array('post','ai1ec_event'); // add custom post types here
}
$query->set('post_type',$post_type);
if (is_category() || is_tag()) {
// Add custom taxonomies to category and tag pages
if (is_category()) {
$taxonomy1 = 'category';
$taxonomy2 = 'events_categories';
}
if (is_tag()){
$taxonomy1 = 'post_tag';
$taxonomy2 = 'events_tags';
}
$queried_object = $query->get_queried_object();
$slug = $queried_object->slug;
$query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => $taxonomy1, 'field' => 'slug', 'terms' => $slug
),
array(
'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
)
));
}
}
}
add_action('pre_get_posts', 'edit_my_query');
I believe this is just the tip of the iceberg for WordPress multiple taxonomy aggregation and taxonomy mapping that will start to occur as custom post types and taxonomies continue to multiply like free plugins and spread through the WordPress ecosystem. We who wish to control what material site visitors and subscribers will have promoted to them regardless of what WordPress has it filed under would like to be able to invisibly and seamlessly assemble that material behind the scenes.
The existing custom post type and taxonomy plugins for WordPress seem to emphasize creating and managing your own set of custom posts, tags and fields, but there seems to be little for managing and presenting the exploding array of custom types being introduced as more and more plugins take advantage of custom post types and taxonomies. A plugin to generate functionality like the above from checkboxes for every post type and taxonomy in the database and a menu of page types would be a handy thing. Plugins that depend on a visitor to navigate taxonomies in a sidebar widget are a waste of time. There’s no reason for a site visitor to think anything more complicated than clicking a menu or category is going on, and this functionality is too useful to restrict to a widget.