I have two custom post types - office and professional. Each professional needs to be tied to an office. I've created a custom taxonomy, 'tax-location', applied it to both post types to tie them together.
I can view each single view of the ctp for office and professional with the templates single-office.php and single-professional.php as expected.
What I now need is to create a page that lists professionals by office on it's own page (think a subpage of the single-office view) and how to get that permalink dynamically. I've tried the taxonomy-$taxonomy-$slug.php template, but can't see to get it to work.
==== My Solution======
I ended up revisiting the taxonomy template solution. My taxonomy slug is tax-location, so I made a taxonomy-tax-location.php template.
To get the term link, I used the get_term_link() and passed the object through the first parameter, which echoes my link to a select field.
$career_terms = get_terms( 'tax-location' );
foreach ( $career_terms as $career_term ) {
$term_link = get_term_link( $career_term, 'tax-location')
echo '<option value="' . $term_link . '">' . $career_term->name . '</option>';
}
From here I used javascript to bounce the user to the selected link that will take them to the taxonomy-tax-location.php template.
Once on the taxonomy-tax-location.php page, I wrote a function that passes the term slug of the queried page to a new WP_Query.
// in taxonomy-tax-location.php
$term = $wp_query->queried_object;
kg_get_careers($term->slug);
// in functions.php
function kg_get_careers($term_slug = '') {
$career_args = array(
'post_type' => 'career',
'posts_per_page' => -1,
'tax-location' => $term_slug
);
$career_posts = new WP_Query( $career_args );
if ( $career_posts->have_posts() ) :
while ( $career_posts->have_posts() ) : $career_posts->the_post();
get_template_part( 'library/partials/part', 'careers-list' );
endwhile;
else:
echo 'No Content';
endif;
wp_reset_postdata();
}
My solution is a bit verbose and I'm sure there's room for improvement, but it works for now.