If I understand correctly, the book and the reviews share same taxonomy.
Then is just a matter of get the taxonomy term for the book in the single-books.php
, save it in a variable and then run a query using that variable for the 'tax_query'
:
if ( have_posts() ) {
the_post();
// get the term for the current book and save in a variable
$book_terms = get_the_terms( get_the_ID(), 'your_custom_taxonomy' );
$book_term = ! empty( $book_terms ) ? array_shift( $book_terms ) : FALSE;
// loop code for the book goes here
}
if ( $book_term ) { // if we get the term for current book...
// second query
$args = array(
'post_type' => 'review',
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy',
'terms' => array( $book_term->term_id )
)
)
);
$reviews = new WP_Query( $args );
// loop code for review goes here
}