I have a template file I am working with that I am having some difficulties with. The template itself works fine. It displays correctly just not the order I desire. My issues comes when it comes time to display the taxonomies in a specific order. I have a meta key set on the taxonomies with the numeric numbers stored in the order I want them to display. What can I do to make my foreach loop through with the order I desire?
<?php
/**
* The template for displaying the years loop.
*/
if ( have_posts() ) :
?>
<?php
// $jposts = array();
if(is_auditor()){
$states = get_auditor_states();
// var_dump($states[0]);
$inc = $states[0];
$years = get_auditor_years();
}else{
$inc = array();
// do nothing
}
if($years){
$inc_years = $years[0];
}else{
$inc_years = array();
}
$j_terms = get_terms([
'taxonomy' => 'jurisdiction',
'include' => $inc,
'hide_empty' => false,
'orderby' => 'slug'
]);
foreach ($j_terms as $j_term){
$jposts[$j_term->term_id]["name"] = $j_term->name;
$jposts[$j_term->term_id]["child"] = array();
$getjposts = get_posts(array(
'post_type' => 'documents',
'numberposts' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'jurisdiction',
'field' => 'term_id',
'terms' => $j_term->term_id, /// Where term_id of Term 1 is "1".
'include_children' => false
),
array(
'taxonomy' => 'years',
'include' => $inc_years,
'field' => 'term_id',
'terms' => $args['term']->term_id, /// Where term_id of Term 1 is "1".
'include_children' => false
)
)
));
if ( $getjposts ) {
?>
<?php echo $j_term->name;?></h3>
<?php
$audiences = array();
foreach ( $getjposts as $post ) :
$audience = get_the_terms( $post->ID, 'audiences' );
foreach($audience as $a){
$audiences[$a->name][] = $post;
}
endforeach;
// Here Im not sure how to numericly order based upon "audiences" meta_key "order"
$a_terms = get_terms(
'taxonomy' => 'audiences',
'hide_empty' => false,
'meta_key' => 'order'
'orderby' => 'meta_value_num'
);
foreach($audiences as $a => $aposts){
echo "<p><span class='dashicons dashicons-tag'></span> ".$a . "</p>";
foreach ( $aposts as $post ) :
setup_postdata( $post );
get_template_part( 'partials/documents', 'index' );
endforeach;
wp_reset_postdata();
}
?>
<hr class="style2 m-3">
<?php
}
}
?>
<?php
?>
<?php
endif;
wp_reset_postdata();
I tried specifying a_terms and looping through that but no luck. I tried using some suggestions on stack but none work for my specific situation. What should happen is when it comes to listing out the audiences it should list in the order specified by the metakey.
source https://stackoverflow.com/questions/76275050/wordpress-taxonomy-orderby-custom-metakey
No comments:
Post a Comment