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

Show list of all posts in a custom taxonomy category on single? WordPress

  • SOLVED

I have a custom post type ("ansatte" - means "employees" in norwegian - there, you even learned some norwegian today!) with a custom taxonomy ("firma" - meaning company - yay, even more norwegian mumbo-jumbo-language!), and on the single-ansatte.php I'd like to show a list of all the employees within the same company as the employee we are currently viewing.

I tried using

$category = get_the_category('slug')

To store the category slug as a variable, and then use the variable within the query, but I'm not sure if this is the correct way to do it. Probably not since it didn't work.


<?php
$category = get_the_category('slug');
$args = array(
'post_type' => 'ansatte',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'firma',
'field' => 'slug',
'terms' => "$category"
)
)

);?>

<?php $my_query = new WP_Query(); $my_query->query($args); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>
<?php endwhile; ?>



Any ideas how to do this?

Since I couldn't get this working, I also tried using conditional tags to show different queries depending on which category/company the employee is in, but it seems in_category and most other conditional tags doesn't work with custom taxonomies?

The conditional tags-approach would also be a less attractive solution, since I'd have to add code if they add another company, but any solution is better than no solution :)

Answers (2)

2011-07-08

Dylan Kuhn answers:

I think you were close, but you need to get the firma terms for the current post and use those in the new query:


<?php
$firma_terms = get_the_terms( 0, 'firma' );
if ( $firma_terms )
$firma_term_slugs = wp_list_pluck( $firma_terms, 'slug' );

$args = array(
'post_type' => 'ansatte',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'firma',
'field' => 'slug',
'terms' => $firma_term_slugs
)
)
);?>

<?php $my_query = new WP_Query(); $my_query->query($args); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>


Torstein Opperud comments:

Works like a charm Dylan! Nice one, thanks!


Torstein Opperud comments:

For the record, I found one small thing that had to be added/changed on the last line:

wp_reset_postdata(); so it became like this

<?php

$firma_terms = get_the_terms( 0, 'firma' );

if ( $firma_terms )

$firma_term_slugs = wp_list_pluck( $firma_terms, 'slug' );

$args = array(

'post_type' => 'ansatte',

'order' => 'ASC',

'posts_per_page' => -1,

'tax_query' => array(

array(

'taxonomy' => 'firma',

'field' => 'slug',

'terms' => $firma_term_slugs

)

)

);?>

<?php $my_query = new WP_Query(); $my_query->query($args); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>

<?php wp_reset_postdata(); endwhile; ?>


Torstein Opperud comments:

Dylan pointed out to me that the last line, <?php wp_reset_postdata(); endwhile; ?> should be done the other way round, with endwhile first, then reset postdata, see dylans code above :)

2011-07-08

Denzel Chia answers:

Hi,

What you need is a taxonomy template.
You cannot do that on single-ansatte.php, this is for single post display.
For displaying a single post of ansatte.

You need a taxonomy-firma-{term}.php with a normal WordPress Loop
Please refer to custom taxonomies display in WordPress Template Hierarchy.
http://codex.wordpress.org/Template_Hierarchy

This is how it works.

You can create a new term which is company name under taxonomy firma and publish it. You can pre-create a few terms "company names" to be reused later. When you create an ansatte post, you can check a company name (For example google) under firma.

For each term "company name" for example, google.
You will create a taxonomy-firma-google.php template with a normal WordPress Loop.
In your (WordPress 3.0) menu, look for this term and tag it onto the menu for quick easy access.
Clicking on this menu item on your web site front end will give you a list of all the employees of google.

This tutorial provides a better understanding of custom taxonomy, but it does not mention taxonomy term template instead it uses taxonomy.php http://net.tutsplus.com/tutorials/wordpress/introducing-wordpress-3-custom-taxonomies/

Thanks.
Denzel


Torstein Opperud comments:

I think you misunderstand what I was trying to do here Denzel. On single-ansatte.php (the template that shows the individual employees' information) I also want a list of the employees that work in the same company as the one we are currently viewing.

But thanks anyway :)