Custom loop with conditional statement to separate each terms

Let’s say for example:
I have a custom taxonomy called “fruit”, which has 3 terms “apple”, “orange”, and “grape”. So in order to get all the posts within all these three terms, I would :

$args = array(
        'post_per_page' => '-1',
    'tax_query' => array(
        array(
            'taxonomy' => 'fruit',
            'field' => 'slug',
            'terms' => array('apple','orange','grape')
        )
    )
);
$query = new WP_Query( $args );

while($query->have_posts()) : $query->the_post();
... Stuff to display from each post
endwhile;

Now the above loop will display all the post from every terms. My question is, how do I separate each terms so I can display Apples inside a div#apple, orange inside a div#orange, and grape inside a div#grape. I am trying to use conditional statement, but I can’t figure out how to…

Read More

@kaiser – My result of var_dump

object(WP_Query)#300 (45) { ["query_vars"]=> array(58) { ["posts_per_page"]=> int(15) ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(12) "psny-contest" ["field"]=> string(4) "slug" ["terms"]=> array(5) { [0]=> string(10) "fall-award" [1]=> string(12) "spring-award" [2]=> string(12) "summer-award" [3]=> string(12) "winter-award" [4]=> string(22) "special-occasion-award" } } } ["error"]=> string(0) "" ["m"]=> int(0) ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["taxonomy"]=> string(12) "psny-contest" ["term"]=> string(10) "fall-award" ["order"]=> string(4) "DESC" } ["tax_query"]=> object(WP_Tax_Query)#298 (2) { ["queries"]=> array(1) { [0]=> array(5) { ["taxonomy"]=> string(12) "psny-contest" ["terms"]=> array(5) { [0]=> string(10) "fall-award" [1]=> string(12) "spring-award" [2]=> string(12) "summer-award" [3]=> string(12) "winter-award" [4]=> string(22) "special-occasion-award" } ["include_children"]=> bool(true) ["field"]=> string(4) "slug" ["operator"]=> string(2) "IN" } } ["relation"]=> string(3) "AND" } ["meta_query"]=> object(WP_Meta_Query)#299 (2) { ["queries"]=> array(0) { } ["relation"]=> NULL } ["post_count"]=> int(15) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> string(2) "20" ["max_num_pages"]=> float(2) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(true) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(true) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> string(32) "36947eefd755aba84b755ec8db4398dc" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["query"]=> array(2) { ["posts_per_page"]=> string(2) "15" ["tax_query"]=> array(1) { [0]=> array(3) { ["taxonomy"]=> string(12) "psny-contest" ["field"]=> string(4) "slug" ["terms"]=> array(5) { [0]=> string(10) "fall-award" [1]=> string(12) "spring-award" [2]=> string(12) "summer-award" [3]=> string(12) "winter-award" [4]=> string(22) "special-occasion-award" } } } } ["request"]=> string(690) " SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type IN ('post_post','post_page','post_picture') JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (92,90,91,89,88) ) AND wp_posts.post_type IN ('post', 'page', 'attachment', 'picture') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND t.language_code='en' GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 15" ["posts"]=> array(15) { [0]=> object(stdClass)#350 (24) { ["ID"]=> int(1086) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:59:46" ["post_date_gmt"]=> string(19) "2012-01-16 08:59:46" ["post_content"]=> string(0) "" ["post_title"]=> string(12) "Skating Rink" ["post_excerpt"]=> string(12) "Skating Rink" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "skating-rink" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 04:01:52" ["post_modified_gmt"]=> string(19) "2012-01-16 09:01:52" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1086" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(stdClass)#351 (24) { ["ID"]=> int(1083) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:58:49" ["post_date_gmt"]=> string(19) "2012-01-16 08:58:49" ["post_content"]=> string(0) "" ["post_title"]=> string(9) "Dirty Boy" ["post_excerpt"]=> string(23) "Profile of a Dirty Boy." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(9) "dirty-boy" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 04:03:10" ["post_modified_gmt"]=> string(19) "2012-01-16 09:03:10" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1083" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [2]=> object(stdClass)#352 (24) { ["ID"]=> int(1080) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:57:45" ["post_date_gmt"]=> string(19) "2012-01-16 08:57:45" ["post_content"]=> string(0) "" ["post_title"]=> string(10) "Night Glow" ["post_excerpt"]=> string(163) "Just another experiment at capturing the northern lights. 2 exposures here. One for the sky, and a little longer one for the water and foreground. I always forget " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "night-glow" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:57:45" ["post_modified_gmt"]=> string(19) "2012-01-16 08:57:45" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1080" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [3]=> object(stdClass)#353 (24) { ["ID"]=> int(1077) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:56:07" ["post_date_gmt"]=> string(19) "2012-01-16 08:56:07" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Cool Landscape" ["post_excerpt"]=> string(14) "Cool Landscape" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(14) "cool-landscape" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:56:07" ["post_modified_gmt"]=> string(19) "2012-01-16 08:56:07" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1077" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [4]=> object(stdClass)#354 (24) { ["ID"]=> int(1074) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:54:40" ["post_date_gmt"]=> string(19) "2012-01-16 08:54:40" ["post_content"]=> string(0) "" ["post_title"]=> string(20) "Waterfalls Landscape" ["post_excerpt"]=> string(43) "Waterfalls Landscape and the flowing river." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(20) "waterfalls-landscape" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-18 21:54:33" ["post_modified_gmt"]=> string(19) "2012-01-19 02:54:33" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1074" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [5]=> object(stdClass)#355 (24) { ["ID"]=> int(1071) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:52:10" ["post_date_gmt"]=> string(19) "2012-01-16 08:52:10" ["post_content"]=> string(0) "" ["post_title"]=> string(16) "Tajmahal Sunrise" ["post_excerpt"]=> string(33) "Tajmahal Sunrise and morning fog." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(16) "tajmahal-sunrise" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:52:10" ["post_modified_gmt"]=> string(19) "2012-01-16 08:52:10" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1071" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [6]=> object(stdClass)#356 (24) { ["ID"]=> int(1068) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:51:19" ["post_date_gmt"]=> string(19) "2012-01-16 08:51:19" ["post_content"]=> string(0) "" ["post_title"]=> string(19) "Side Road Landscape" ["post_excerpt"]=> string(52) "Side Road Landscape of a beautiful highway in Tiwan." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(19) "side-road-landscape" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:51:19" ["post_modified_gmt"]=> string(19) "2012-01-16 08:51:19" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1068" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [7]=> object(stdClass)#357 (24) { ["ID"]=> int(1065) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:50:16" ["post_date_gmt"]=> string(19) "2012-01-16 08:50:16" ["post_content"]=> string(0) "" ["post_title"]=> string(13) "Beautiful Sky" ["post_excerpt"]=> string(40) "Beautiful Sky and beautiful green grass." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "beautiful-sky" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:50:16" ["post_modified_gmt"]=> string(19) "2012-01-16 08:50:16" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1065" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [8]=> object(stdClass)#358 (24) { ["ID"]=> int(1062) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:48:31" ["post_date_gmt"]=> string(19) "2012-01-16 08:48:31" ["post_content"]=> string(0) "" ["post_title"]=> string(20) "Portrait Photography" ["post_excerpt"]=> string(36) "Portrait Photography of a lonly boy." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(20) "portrait-photography" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:48:31" ["post_modified_gmt"]=> string(19) "2012-01-16 08:48:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1062" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [9]=> object(stdClass)#359 (24) { ["ID"]=> int(1057) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:45:55" ["post_date_gmt"]=> string(19) "2012-01-16 08:45:55" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "Paint photography" ["post_excerpt"]=> string(36) "Paint phtography of a wonderful day." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(16) "paint-phtography" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:47:24" ["post_modified_gmt"]=> string(19) "2012-01-16 08:47:24" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1057" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [10]=> object(stdClass)#360 (24) { ["ID"]=> int(1054) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:44:28" ["post_date_gmt"]=> string(19) "2012-01-16 08:44:28" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "Statue of Liberty" ["post_excerpt"]=> string(29) "Statue of Liberty silhouette " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(17) "statue-of-liberty" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:44:28" ["post_modified_gmt"]=> string(19) "2012-01-16 08:44:28" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1054" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [11]=> object(stdClass)#361 (24) { ["ID"]=> int(1050) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:43:20" ["post_date_gmt"]=> string(19) "2012-01-16 08:43:20" ["post_content"]=> string(0) "" ["post_title"]=> string(16) "New York Skyline" ["post_excerpt"]=> string(43) "New York Skyline looking west at night sky." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(16) "new-york-skyline" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:43:20" ["post_modified_gmt"]=> string(19) "2012-01-16 08:43:20" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1050" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [12]=> object(stdClass)#362 (24) { ["ID"]=> int(1047) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:41:16" ["post_date_gmt"]=> string(19) "2012-01-16 08:41:16" ["post_content"]=> string(0) "" ["post_title"]=> string(30) "Infrared Photography Fisherman" ["post_excerpt"]=> string(50) "Infrared Photography Fisherman fishing by himself." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(30) "infrared-photography-fisherman" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:41:16" ["post_modified_gmt"]=> string(19) "2012-01-16 08:41:16" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1047" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [13]=> object(stdClass)#363 (24) { ["ID"]=> int(1044) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:38:14" ["post_date_gmt"]=> string(19) "2012-01-16 08:38:14" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "High Speed Bullet" ["post_excerpt"]=> string(40) "High Speed Bullet going through crayons." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(17) "high-speed-bullet" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 03:38:14" ["post_modified_gmt"]=> string(19) "2012-01-16 08:38:14" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1044" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [14]=> object(stdClass)#364 (24) { ["ID"]=> int(1041) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:35:55" ["post_date_gmt"]=> string(19) "2012-01-16 08:35:55" ["post_content"]=> string(0) "" ["post_title"]=> string(22) "Grand Central Entrance" ["post_excerpt"]=> string(30) "Grand Central Entrance statue." ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(22) "grand-central-entrance" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-17 13:14:52" ["post_modified_gmt"]=> string(19) "2012-01-17 18:14:52" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1041" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } } ["post"]=> object(stdClass)#350 (24) { ["ID"]=> int(1086) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-01-16 03:59:46" ["post_date_gmt"]=> string(19) "2012-01-16 08:59:46" ["post_content"]=> string(0) "" ["post_title"]=> string(12) "Skating Rink" ["post_excerpt"]=> string(12) "Skating Rink" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "skating-rink" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-01-16 04:01:52" ["post_modified_gmt"]=> string(19) "2012-01-16 09:01:52" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(49) "http://psnyusa.org/?post_type=picture&p=1086" ["menu_order"]=> int(0) ["post_type"]=> string(7) "picture" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }

Related posts

Leave a Reply

1 comment

  1. Here is how you can group your post listing based on their terms. First you need to add the following code to your functions.php

    // join the term_taxonomy table so we can get the term id with the post
    add_filter( 'posts_join_paged', 'wpse39380_posts_join', 10, 2 );
    function wpse39380_posts_join( $join, $query ) {
            // you need to add your check here so not all queries are modified
        if ( $query->is_archive && $query->tax_query ) {
            $join .= " LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_taxonomy_id";
        }
        return $join;
    }
    // order the posts by term_id
    add_filter('posts_orderby', 'wpse39380_orderby_terms', 10, 2 );
    function wpse39380_orderby_terms( $orderby, $query ) {
            // you need to add your check here so not all queries are modified
        if ( $query->is_archive && $query->tax_query ) {
            return ' wp_term_taxonomy.term_id ';
        }
        return $orderby;
    }
    // add the term_id to select fields
    add_filter( 'posts_fields', 'wpse39380_posts_field', 10, 2 );
    function wpse39380_posts_field( $fields, $query ) {
            // you need to add your check here so not all queries are modified
        if ( $query->is_archive && $query->tax_query ) {
            $fields .= ", wp_term_taxonomy.term_id";
            return $fields;
        }
        return $fields;
    }
    

    Now your custom loop will look like following:

    $args = array(
        'post_per_page' => '-1',
        'tax_query' => array(
            array(
                'taxonomy' => 'fruit',
                'field' => 'slug',
                'terms' => array('apple','orange','grape')
            )
        )
    );
    $query = new WP_Query( $args );
    
    $current_term = null;   // to keep track of term 
    while($query->have_posts()) : $query->the_post();
                global $post;
                // if we have current_term set
                // and current_term not equal to the current post term
                if ( $current_term && $current_term != $post->term_id ) {
                    echo '</div>';
                    $current_term = null;
                }
    
                // if current_term is null then either its the first iteration
                // or the last term posts have been displayed
                if ( $current_term === null ) {
                    $current_term = $post->term_id;
                    $term = get_term( ( int ) $current_term, 'fruit' );
                    echo "<div id='$term->name'>";
                    echo "<h2>$term->name</h2>";
                }
                the_title(); 
                echo '<br />';
    
    endwhile;
    if ( $current_term ) {
        echo '</div>';
    }