pagename
is for request page (core page
post type) by slug. What you want, if I understood correctly, is to get “attorney” posts that belongs to same case-log
terms that current “case” post. I would do it something like this (not sure where you are going to execute the code, I’ve tried to code as universally as possible):
if( is_singular( "case" ) ) {
//We are in single "case" view
//Get the ID of current case post
$case_id = get_queried_object_id();
// Set "post_type" agument for the query
$args = array(
'post_type' => 'attorney'
);
//Get the terms from case-log taxonomy
//get_the_terms returns an array of term objects, false or a WP_Error object
$terms = get_the_terms( $case_id, 'case-log' );
if( ! is_wp_error( $terms ) && $terms ) {
$terms_ids = array();
foreach( $terms as $term ) {
$terms_ids[] = $term->term_id;
}
// Set "tax_query" agument for the query
$args['tax_query'] = array(
array(
'taxonomy' => 'case-log',
'terms' => $terms_ids
),
);
}
$case_attorney = new WP_Query( $args );
if ( $case_attorney->have_posts() ) {
while( $case_attorney->have_posts() ) {
$case_attorney->the_post();
?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
</div>
<?php
}
wp_reset_postdata();
} else {
echo "No matching attorney";
}
}