Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created October 4, 2010 21:58
Show Gist options
  • Save mikeschinkel/610526 to your computer and use it in GitHub Desktop.
Save mikeschinkel/610526 to your computer and use it in GitHub Desktop.
<?php
/*
* Adds "tax_terms" to WP_Query()
*
* See: http://lists.automattic.com/pipermail/wp-hackers/2010-October/035258.html
*
* Author: Mike Schinkel (http://mikeschinkel.com)
*
*/
header('Content-Type:text/plain');
include "../wp-load.php";
add_action('posts_where','tax_terms_where',10,2);
$result = new WP_Query('post_type=show&tax_terms=genre:grunge,genre:alternate');
foreach($result->posts as $post) {
echo "{$post->post_title}\n";
}
function tax_terms_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) && isset($query['tax_terms'])) {
global $wpdb;
$tax_terms = explode(',',$query['tax_terms']);
foreach($tax_terms as $tax_term) {
list($taxonomy,$term) = explode(':',$tax_term);
$sql = <<<SQL
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
INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id
WHERE tt.taxonomy='%s' AND t.slug='%s'
)
SQL;
$where .= $wpdb->prepare($sql,$taxonomy,$term);
}
}
}
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment