Posting my solution if anyone has a similar need or if anyone thinks of a more elegant solution (I’m sure there is one). I would love for someone to come up with a way to make this happen in one template instead of one for each term:
1. created a template file for each of the taxonomy terms in the CPT from a clone of the main CPT template (e.g. single-cpt-term1.php, single-cpt-term2.php, etc.).
2. added meta boxes to the CPT to assign a value for each of the terms
3. ordered by the meta value in each of the individual term templates
<?php //get the post's assignment
$custom_terms = wp_get_post_terms($post->ID, 'your_custom_taxonomy');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through assignments and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'your_custom_posttype',
'posts_per_page' => 6,
'meta_key' => 'your_metakey',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'post__not_in' => array( get_the_ID() ), // Exclude current post
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- YOUR CONTENT HERE -->
<?php endwhile;
}
wp_reset_query();
}?>
-
This reply was modified 7 years, 8 months ago by
wrimomatt.
-
This reply was modified 7 years, 8 months ago by
wrimomatt. Reason: code spacing is weird