I have a page which lists all tags (artist names) for a certain category along with links to that artists work. Each post only has one tag if that helps simplify things.

I am trying to order the tags by the last name rather than the first. An example of the tag might be "Claude Monet" - but currently that is sorted as "C" rather than by "M".

The closest post with a solution is at Taxonomy terms sort by... Last name! however I can't get that to work.

Currently my code is as follows:

$post_tags = array();
$the_query = new WP_Query( 'posts_per_page=-1&cat=28' );
if ( $the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        $posttags = get_the_tags();
        foreach( $posttags as $posttag )  {
            $post_tags[$posttag->slug] = $posttag->name;

<ul class="artistList" data-filter-group="tags">
    <?php  natcasesort($post_tags);
    foreach( $post_tags as $tagslug => $tagname ){ ?>
        <li><a href="<?php echo site_url(); ?>/for-sale/<?php echo $tagslug;?>" data-filter-value="<?php echo     $tagslug;?>"><?php echo $tagname;?></a></li>
    <?php } ?>    

I am not very skilled with this stuff. From the previously linked taxonomy article it seems like I could make a filter in the functions file to ensure a sort by last word (wpse_last_word) and then use the array sort, but I am stumped at actually executing if that is indeed a valid solution.

Where am I going wrong?

Thanks much in advance

Your Answer

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

Browse other questions tagged or ask your own question.