Forums

Display the most recently added tags (7 posts)

  1. SpitefulBadger
    Member
    Posted 1 year ago #

    I am trying to select the most recently added tags from wordpress and display them in alpha order as links to that tag page.

    I've got everything except putting them in alpha order. This code displays the 5 most recently added tags in a nice list as links. But they're in order of the term_id.

    Do I have to put them into an array and re-sort them by name? If so any help on how to do that would be appreciated.

    <?php
    $tags = get_tags('number=5&orderby&order=DESC');
    if ($tags) {
    foreach ($tags as $tag) {
    echo '<br><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a> </p> ';
    }
    }
    ?>
  2. ambrosite
    Member
    Posted 1 year ago #

    You could try changing the first line as follows:

    $tags = get_tags('number=5&orderby=name&order=DESC');

    However, I think you will find that this change causes a different set of tags to be returned. It is not actually possible to sort the tags by date, because WordPress does not store the date that the tag was added in the database. Your code only appears to work that way, because you are sorting on term_id in descending order, and term_id is an auto-incrementing field.

    One way to make this work would be to put the term_id and name into an associative array, sort the array, and then use the sorted array in the foreach loop that outputs the tag links.

  3. Mark / t31os
    Moderator
    Posted 1 year ago #

    Leave out the orderby parameter and get_tags will order by term_id, the higher the ID, the newer the tag, so technically that's the sort order you need, just keep the order direction (ie. order=DESC) to ensure you get newest first.

  4. SpitefulBadger
    Member
    Posted 1 year ago #

    I am pulling the tags I want, the most recent ones, I just need to now display them in alpha order.

    Would I put them in the associative array to then sort them in alpha order?

    If so, how do I put them into an array?

  5. ambrosite
    Member
    Posted 1 year ago #

    The basic problem is that get_tags returns an array of objects, but you cannot sort an array of objects using the standard PHP sorting functions.

    Here is what I meant by putting the tag data into an associative array and then sorting it. This works because you only need two pieces of data (tag ID and tag name) to print the links. Otherwise, you would have to use usort and define a custom comparison function to sort the tag objects.

    <?php
    $tags = get_tags('number=5&orderby&order=DESC');
    
    if ($tags) {
    	$tag_data = array();
    	foreach ($tags as $tag) {
    		$tag_data[$tag->term_id] = $tag->name;
    	}
    
    	asort($tag_data);
    
    	foreach ($tag_data as $tag_id => $tag_name) {
    		echo '<br><a href="' . get_tag_link( $tag_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag_name ) . '" ' . '>' . $tag_name.'</a> </p> ';
    	}
    }
    ?>
  6. SpitefulBadger
    Member
    Posted 1 year ago #

    Ah, I see. Thanks for the help!

  7. ambrosite
    Member
    Posted 1 year ago #

    You're welcome. As a point of interest, here is the same solution using usort. You would have to do it this way if you needed more than two properties of the tag object in your foreach loop -- for example, if you wanted to use the tag description as the link title.

    <?php
    $tags = get_tags('number=5&order=DESC');
    
    if ($tags) {
            usort( $tags, create_function('$a,$b', 'return strcmp($a->name, $b->name);') );
    
            foreach ($tags as $tag) {
                    echo '<br><a href="' . get_tag_link( $tag->term_id ) . '" title="' . $tag->description . '" ' . '>' . $tag->name.'</a> </p> ';
            }
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic