Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This is an example showing a hook used to add the ability to use "any" as a term to match all terms for a taxonomy.
It will run as a standalone file in the root of a WordPress 3.0 install with a URL that looks like:
It was written to address this question:
Be sure to edit it to change the values for $post_type and $taxonomy to match the ones you need.
include "wp-load.php";
//======[CHANGE THESE]=========
$post_type = 'portfolio_item';
$taxonomy = 'clients';
$posts = new WP_Query("post_type=$post_type&taxonomy=$taxonomy&term=any&posts_per_page=-1");
if ( $posts->have_posts() ):
while ( $posts->have_posts() ) : $posts->the_post(); ?>
<h2 id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php the_title(); ?></h2>
<?php //the_content();
function my_posts_where($where,$query) {
if (isset($query->query_vars['taxonomy']) && $query->query_vars['term']=='any') {
global $wpdb;
$tt = $wpdb->term_taxonomy;
$tr = $wpdb->term_relationships;
$where .= $wpdb->prepare(" AND {$wpdb->posts}.ID IN (
SELECT object_id FROM $tr
INNER JOIN $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id
WHERE $tt.taxonomy=%s) ",$query->query_vars['taxonomy']);
$where = str_replace(' AND 0 ','',$where);
return $where;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment