[wp-hackers] Query Taxonomy in Pages

Mike Schinkel mikeschinkel at newclarity.net
Tue Aug 10 12:51:16 UTC 2010


Devin,

I believe this can handle your "OR" of page tags:

add_filter('posts_where','my_posts_where',10,2 );
function my_posts_where( $sql_where,$query ) {
	global $wpdb;
	if (!empty($query->query['page-tags'])) {
		$page_tags = explode(',',$query->query['page-tags']);
		$slug_templates = implode(',',array_fill(0,count($page_tags),'%s'));
		$add_where = <<<SQL
AND {$wpdb->posts}.ID IN (SELECT tr.object_id
FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms t ON t.term_id = tt.term_id WHERE t.slug IN ($slug_templates))
SQL;
		$add_where = $wpdb->prepare($add_where,$page_tags);
	}
	return "$sql_where $add_where ";
}


On Aug 9, 2010, at 2:05 PM, Devin Dixon wrote:

> Hey all,
> 
> I am trying to query pages based on taxonomy. The pages have a custom
> taxonomy called 'page-tags' . The followings arguments work when
> passed into  WP_Query().
> 
> $args=array(
>  'page-tags' => 'basic tag' ,
>  'post_type' => 'page',
>  //'post_status' => 'publish',
>  //'posts_per_page' => -1,
>  'caller_get_posts'=> 1
> );
> 
> This will pull all the pages with the tag 'basic tag', it works. Now
> the problem comes into play when I try to add in more than one tags.
> For example:
> 
> $args=array(
>  'page-tags' => "basic tag, 2nd tag" ,
>  'post_type' => 'page',
>  //'post_status' => 'publish',
>  //'posts_per_page' => -1,
>  'caller_get_posts'=> 1
> );
> 
> Then nothing is return in the query.  i've also tried using
> 'page-tags' => array('basic tag', '2nd tag')
> 
> and that still doesn't work. So my question is, how can I use an OR
> for the taxonomy?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list