Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created July 29, 2010 17:13
Show Gist options
  • Save mikeschinkel/498700 to your computer and use it in GitHub Desktop.
Save mikeschinkel/498700 to your computer and use it in GitHub Desktop.
<?php
/*
* Example code showing how to hook WordPress 3.0 to allow for querying multiple taxonomies
* Uses two (2) approaches:
*
* 1. A "tax_terms" URL parameter: WP_Query('tax_terms=type:dining,city:mb,package:discounts')
* 2. Taxonomies as URL parameters: WP_Query('type=dining&city=mb&package=discounts')
*
* To try this example:
*
* 1. Modify this example $post_type and $tax_terms1 and $tax_terms2 variables for your use-cases.
* 2. Name the edited file "test.php" and put in the root of your website.
* 3. Test by typing one of the following in your browser's URL field where you use your domain instead of "example.com":
* a. http:/example.com/test.php
* b. http:/example.com/test.php?type2
*
* By:
*
* Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
*
* This example is licensed GPLv2.
*
*/
include "wp-load.php";
add_filter('posts_where','my_posts_where',10,2);
$post_type = 'ticket';
$tax_terms1 = 'tax_terms=type:dining,city:mb,package:discounts';
$tax_terms2 = 'type=dining&city=mb&package=discounts';
$base_query = "post_type=$post_type&posts_per_page=160&orderby=title&order=asc";
if (isset($_GET['type2']))
$posts = new WP_Query("$tax_terms2&$base_query");
else
$posts = new WP_Query("$tax_terms1&$base_query");
echo "<h1>Query:</h1>";
echo '<pre>';
parse_str($posts->query,$query);
print_r($query);
echo '</pre>';
echo "<h1>Results:</h1>";
while ( $posts->have_posts() ):
$posts->the_post(); ?>
<h2 id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php the_title(); ?></h2>
<?php the_content();
echo '<hr/>';
endwhile;
function my_posts_where($where,$wp_query) {
if (isset($wp_query->query)) {
$query = $wp_query->query;
if (is_string($query))
parse_str($query,$query);
if (is_array($query)) {
$post_type = (isset($query['post_type']) ? $query['post_type'] : 'any');
foreach(get_taxonomies(false,'objects') as $taxonomy => $taxonomy_obj) {
if (isset($query[$taxonomy]) && ($post_type=='any' || in_array($post_type,$taxonomy_obj->object_type))) {
$where .= get_tax_term_sql_where($taxonomy,$query[$taxonomy]);
}
}
if (isset($query['tax_terms'])) {
$tax_terms = explode(',',$query['tax_terms']);
foreach($tax_terms as $tax_term) {
list($taxonomy,$term) = explode(':',$tax_term);
$where .= get_tax_term_sql_where(trim($taxonomy),trim($term));
}
}
}
}
return $where;
}
function get_tax_term_sql_where($taxonomy,$term) {
global $wpdb;
if (is_numeric($term)) {
$term_id = $term;
} else {
$term_obj = get_term_by('slug',$term,$taxonomy);
$term_id = $term_obj->term_id;
}
$sql_where = " AND {$wpdb->posts}.ID IN (
SELECT tr.object_id
FROM {$wpdb->term_relationships} AS tr
INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy='$taxonomy' AND tt.term_id=$term_id
) ";
return $sql_where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment