Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
wp-all-posts-for-any-term-in-a-taxonomy.php
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:
http://example.com/wp-all-posts-for-any-term-in-a-taxonomy.php
It was written to address this question:
http://lists.automattic.com/pipermail/wp-hackers/2010-July/033465.html
Be sure to edit it to change the values for $post_type and $taxonomy to match the ones you need.
*/
include "wp-load.php";
add_action('posts_where','my_posts_where',10,2);
//======[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();
endwhile;
endif;
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
You can’t perform that action at this time.