0

I am trying everything to get my custom taxonomy to display alphabetical but nothing seems to work. I have added orderby=title and name but the tax list still displays the order of how it was posted. This is what I have going on. The snippet is used to display current post information based on the selected tax. I have two sets of tax assigned to a post (year and category) on the front end once I click the year and the post I want to view it displays all the information along with the category list. The category list is controlled as well as a tax but for some reason I am unable to get this list to display alphabetical but instead it is posted based on publish date. Below is my code

 function get_related_posts( $taxonomy = '', $args = array() ) {
if ( !is_single() )
    return false;
/* Check if we have a valid taxonomy*/
if ( !$taxonomy ) 
    return false;
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !taxonomy_exists( $taxonomy ) )
    return false;
$current_post = get_queried_object();

/* Get the post terms, just the ids*/
$terms = wp_get_post_terms( $current_post->ID, $taxonomy, array( 'fields' => 'ids') );
/* Lets only continue if we actually have post terms and if we don't have an WP_Error object. If not, return false*/
if ( !$terms || is_wp_error( $terms ) )
    return false;
/* Set the default query arguments*/
$defaults = array(
    'post_type' => $current_post->post_type,
    'post__not_in' => array( $current_post->ID),
    'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'terms' => $terms,
            'include_children' => false,
            'orderby' => 'title',
        ),
    ),
);
/* Validate and merge the defaults with the user passed arguments*/
if ( is_array( $args ) ) {
    $args = wp_parse_args( $args, $defaults );
} else {
    $args = $defaults;
}

$q = get_posts( $args );

return $q;
 }
if ( function_exists( 'get_related_posts' ) ) {
    $related_posts = get_related_posts( 'juryyear', array( 'posts_per_page' => 99,) );
    if ( $related_posts ) {
        foreach ( $related_posts as $post ) {
            setup_postdata( $post );
?>
            <li><a href="<?php echo get_permalink( $post->ID ); ?>"><?php $terms = get_the_terms( $post->ID , 'jury_category' ); foreach ( $terms as $term ) { echo $term->name; }?></a></li>
<? } wp_reset_postdata(); } }?>
        </ul>
    </li>
</ul>
0

I figured it out. I needed to add 'orderby' => 'menu_order', 'order' => 'ASC', to $related_posts = get_related_posts( 'juryyear', array( 'posts_per_page' => 99,) );

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.