Default Green Orange
Swifty's Blog
James Irving-Swift's portfolio and random blogging
RSS
  • Home Page Home
  • My CV

Archive for October 7th, 2010

Filtering custom posts by more than one Taxonomy

Development, Wordpress 0 Comment »

With WordPress now able to use custom post types, I have asked on forums if anyone new how to filter by more than one taxonomy. I originally tried using

 query_posts('cat=6,5') 

and other similar commands but have no luck.

To resolve this I came up with the following solution:

First I create two custom fields on the page which I named taxonomy1 and taxonomy2.
I then added the following code:

 <?php 
$taxonomy1 = get_post_meta($post->ID, "taxonomy1"); 
$taxonomy2 = get_post_meta($post->ID, "taxonomy2");

//this is an sql query to find all posts containing two taxonomies
$sql_query = "SELECT * FROM (
			SELECT wp_posts.ID FROM `wp_posts` 
				JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
				JOIN wp_terms ON wp_term_relationships.term_taxonomy_id=wp_terms.term_id
					WHERE slug = '".$taxonomy1[0]."' 
			UNION ALL
			SELECT wp_posts.ID FROM `wp_posts` 
				JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
				JOIN wp_terms ON wp_term_relationships.term_taxonomy_id=wp_terms.term_id
					WHERE post_type = 'tiles' AND slug = '".$taxonomy2[0]."'
		) AS results

		GROUP BY results.id HAVING COUNT(*) = 2";
		
	$newresults = $wpdb->get_results($sql_query);			
		
	$ArrayOfIDs = array();
		
	foreach($newresults as $newresult){
		array_push($ArrayOfIDs, $newresult->ID);
	}

	query_posts(array('posts_per_page'=>'900','post_type' => 'tiles','post__in' => $ArrayOfIDs));
?>

The idea behind this code is that the mysql query returns the post IDs of the posts that correspond to the two taxonomy filters and then the foreach loop pushes those into an array of IDs ($ArrayOfIDs) which is then used in the query_post function.

Feedback would be much appreciated on other ways that I could have resolved this issue or also how I could improve this code.


October 7th, 2010  



  • Search

  • Calendar

    October 2010
    M T W T F S S
    « Sep   May »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • My Twitter

    • @skyatlantic why is sky atlantic HD the only channel that apparently can get signal?!? 2 days ago
    • @miniviolette it was… I realised that I was to drunk to continue when I only got one kill in battlefield3 in 30 mins!!!! epic fail! 4 days ago
    • @miniviolette yeah! i just stay in an played xbox and didn't pay attention to how much I was drinking at the same time! Amateur Mistake! 4 days ago
    • This sums up my Saturday morning! http://t.co/yBuMQ0afMy 4 days ago
    • @Rich_McCartney I'm still on Lion, i really need to sort out mountain lion so that my thunderbolt display stops being buggy! 5 days ago
  • Categories

    • Development
    • Development
    • JISCMail
    • Just for fun
    • Linux
    • Magento
    • Mailtalk
    • Mutiny
    • Random
    • Thomas Murr Art
    • Ubuntu
    • Websites
    • Wordpress
  • Archives

    • May 2012
    • March 2012
    • November 2011
    • May 2011
    • October 2010
    • September 2010
    • July 2010
    • April 2010
    • March 2010
    • SN205662 Samsung digital camera
    • SN205661 Samsung digital camera
    • SN205660 Samsung digital camera
    • SN205658 Samsung digital camera
    • SN205659 Samsung digital camera
    • SN205657 Samsung digital camera
    • SN205656 Samsung digital camera
    • SN205655 Samsung digital camera
    • SN205654 Samsung digital camera
    • SN205653 Samsung digital camera
    • SN205651 Samsung digital camera
    • SN205652 Samsung digital camera
    • SN205650 Samsung digital camera
    • SN205649 Samsung digital camera
    • SN205648 Samsung digital camera
    • SN205644 Samsung digital camera
    • SN205625 Samsung digital camera
    • SN205639 Samsung digital camera
    • SN205626 Samsung digital camera
    • SN205636 Samsung digital camera
Copyright © 2013 Swifty's Blog
XHTML CSS Log in