Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Taxonomies with Categories WordPress

Hi All,

I have created a taxonomy within "Posts" that allows an author to be tagged (I know WordPress has the authors functionality built in, but the client didn't want to use this as they didn't want to add hundreds of email addresses).

I have now added the functionality to display all of those authors and the authors information on the category page. This is working fine.

However, the client would like the authors only listed once they have posted within a certain category! Any ideas how this is done?

Currently I have the following code:

<?php
$myterms2 = get_terms('author', $args2);
$args2 = array('public'=>true,'_builtin' => false,'order'=>'ASC','hide_empty'=>false);
foreach($myterms2 as $term2) { ?>
<?php $t_id = $term2->term_id; ?>
<?php $term_meta = get_option( "taxonomy_$t_id" ); ?>
<div class="author_small">
<?php if ( $term_meta['author_image'] == '' ) { ?><div class="ava"><img src="<?php bloginfo('template_url'); ?>/images/avatar_default.jpg" alt="" height="37" width="29" /></div><!--ava--><?php } else { ?><div class="ava"><a href="<?php bloginfo('url'); ?>/author/<?php echo $term2->slug; ?>"><img src="<?php echo $term_meta['author_image']; ?>" alt="" height="37" width="29" /></a></div><!--ava--><?php } ?>
<h6><a href="<?php bloginfo('url'); ?>/author/<?php echo $term2->slug; ?>"><?php echo $term2->name; ?></a></h6>
<div class="text_content">
<p><?php echo $term_meta['author_short_desc']; ?></p>
</div><!--text_content-->
</div><!--author_small-->
<?php } ?>

Any help would be massively appreciated.

Answers (2)

2013-05-30

John Cotton answers:

Ignore - that was nonsense.


John Cotton comments:

You could use get_objects_in_term to get a list of posts with the category they must have posted to and then use wp_get_object_terms on that list of objects to get the "authors" taxonomy terms for those posts.


craigfarrall comments:

Any ideas on how to do this? Any code examples?


John Cotton comments:

Remy's code looks good.

2013-05-30

Remy answers:

To elaborate on what John said, and using your code above :


<?php
$posts_in_category = get_objects_in_term( $category_id, 'category' );
$authors = wp_get_object_terms( $posts_in_category, 'author' );

if(!empty($authors)){
if(!is_wp_error( $authors )){
foreach($authors as $term2) { ?>
<?php $t_id = $term2->term_id; ?>
<?php $term_meta = get_option( "taxonomy_$t_id" ); ?>
<div class="author_small">
<?php if ( $term_meta['author_image'] == '' ) { ?><div class="ava"><img src="<?php bloginfo('template_url'); ?>/images/avatar_default.jpg" alt="" height="37" width="29" /></div><!--ava--><?php } else { ?><div class="ava"><a href="<?php bloginfo('url'); ?>/author/<?php echo $term2->slug; ?>"><img src="<?php echo $term_meta['author_image']; ?>" alt="" height="37" width="29" /></a></div><!--ava--><?php } ?>
<h6><a href="<?php bloginfo('url'); ?>/author/<?php echo $term2->slug; ?>"><?php echo $term2->name; ?></a></h6>
<div class="text_content">
<p><?php echo $term_meta['author_short_desc']; ?></p>
</div><!--text_content-->
</div><!--author_small-->
<?php }
}
} ?>


$category_id being the category your client wants


craigfarrall comments:

Hi Remy,

Thanks for that code above, that works almost perfectly.

One slight bug - If an author posts more than once in a category it displays there details again. So if they have posted five times for example it will show the details five times.

Any ideas how to fix this?


Remy comments:

I think you can filter duplicates with
$authors = array_unique( wp_get_object_terms( $posts_in_category, 'author' ) );


craigfarrall comments:

HI Remy,

That code causes any error, any ideas?


Remy comments:

What kind of error ?


craigfarrall comments:

No specific error appears, it just doesn't load anything else below it, which is cause because of an error.

Any ideas?


Remy comments:

2 things :
- you can set WP_DEBUG to true in wp-config.php to display the errors
- you can remove the array_unique function, and do a print_r( $posts_in_category) and print_r( $authors) and give us the result to be sure about how it works


craigfarrall comments:

I have turned on WP_DEBUG and it didn't show any errors, but is clearly breaking somewhere.

I am thinking I might not have the newest PHP and this bit of code isn't included within the version I am running.

Any other ways around this?


Remy comments:

array_unique is here since PHP 4, it would be quite surprising you don't have at least PHP5 now. I'd like to see what the print_r on $authors and $posts_in_category returns to understand where it's failing


craigfarrall comments:

Hi Remy,

I have now printed those bits, this is what appears:

Array ( [0] => 297 [1] => 1283 [2] => 1314 [3] => 1535 [4] => 1896 [5] => 2000 [6] => 2284 [7] => 2439 [8] => 2460 [9] => 2493 [10] => 2549 [11] => 2788 [12] => 2795 [13] => 2807 [14] => 2840 [15] => 2874 [16] => 2879 [17] => 3234 [18] => 3248 [19] => 3730 [20] => 4410 [21] => 4464 [22] => 4475 [23] => 7173 [24] => 8586 [25] => 8662 [26] => 8669 [27] => 8679 [28] => 8718 [29] => 10567 ) Array ( [0] => stdClass Object ( [term_id] => 339 [name] => Colin Humphreys [slug] => colin-humphreys [term_group] => 0 [term_taxonomy_id] => 1774 [taxonomy] => author [description] => Colin Humphreys is author of 'The Mystery of the Last Supper' (2011) and director of research at the Department of Materials Science and Metallurgy at the University of Cambridge. He was awarded a knighthood in 2010 for services to science. [parent] => 0 [count] => 1 ) [1] => stdClass Object ( [term_id] => 1711 [name] => David H. Levy [slug] => david-h-levy [term_group] => 0 [term_taxonomy_id] => 1729 [taxonomy] => author [description] => David H. Levy is President of the National Sharing the Sky Foundation, and is one of the most successful comet discoverers in history. He has discovered 23 comets (eight of them using his own backyard telescopes) and was co-discoverer of Shoemaker–Levy 9, the comet that collided with Jupiter in 1994 producing the most spectacular explosions ever witnessed in the Solar System. Asteroid 3673 (Levy) was named in his honor. He has written three dozen books, is a contributing editor and monthly columnist for Astronomy, and was the former Science Editor for Parade magazine. In 1998 he won an Emmy as part of the writing team for the Discovery Channel documentary, Three Minutes to Impact. He now holds a Ph.D. from the Hebrew University of Jerusalem in the field of literature and the night sky. [parent] => 0 [count] => 2 ) [2] => stdClass Object ( [term_id] => 1711 [name] => David H. Levy [slug] => david-h-levy [term_group] => 0 [term_taxonomy_id] => 1729 [taxonomy] => author [description] => David H. Levy is President of the National Sharing the Sky Foundation, and is one of the most successful comet discoverers in history. He has discovered 23 comets (eight of them using his own backyard telescopes) and was co-discoverer of Shoemaker–Levy 9, the comet that collided with Jupiter in 1994 producing the most spectacular explosions ever witnessed in the Solar System. Asteroid 3673 (Levy) was named in his honor. He has written three dozen books, is a contributing editor and monthly columnist for Astronomy, and was the former Science Editor for Parade magazine. In 1998 he won an Emmy as part of the writing team for the Discovery Channel documentary, Three Minutes to Impact. He now holds a Ph.D. from the Hebrew University of Jerusalem in the field of literature and the night sky. [parent] => 0 [count] => 2 ) [3] => stdClass Object ( [term_id] => 1137 [name] => Paul Kinzer [slug] => paul-kinzer [term_group] => 0 [term_taxonomy_id] => 1835 [taxonomy] => author [description] => Paul Kinzer is author of 'Stargazing Basics' (2008). [parent] => 0 [count] => 3 ) [4] => stdClass Object ( [term_id] => 1137 [name] => Paul Kinzer [slug] => paul-kinzer [term_group] => 0 [term_taxonomy_id] => 1835 [taxonomy] => author [description] => Paul Kinzer is author of 'Stargazing Basics' (2008). [parent] => 0 [count] => 3 ) [5] => stdClass Object ( [term_id] => 1137 [name] => Paul Kinzer [slug] => paul-kinzer [term_group] => 0 [term_taxonomy_id] => 1835 [taxonomy] => author [description] => Paul Kinzer is author of 'Stargazing Basics' (2008). [parent] => 0 [count] => 3 ) [6] => stdClass Object ( [term_id] => 1728 [name] => Rachel E. [slug] => rachel-e [term_group] => 0 [term_taxonomy_id] => 1786 [taxonomy] => author [description] => Rachel is a book publicist at Cambridge. Follow her @CambridgeUP_NY. [parent] => 0 [count] => 1 ) )

Any further ideas?