0

I'm displaying a lit of tags when they have a description. I'd like to sort them by name instead of by post quantity. Is there a way to do this?

<?php 
    $tags = get_tags();
    if ($tags) {
      foreach ($tags as $tag) {
        if ($tag->description) {
          echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '><h3>' . $tag->name.'</h3></a></dt><dd style="margin-bottom: 10px;">' . $tag->description . '</dd>';
        }
      }
    }
?>

2 Answers 2

1

You should use the function wp_tag_cloud, it has many parameters as described in the codex and the one to sort the results as you want: 'orderby' => 'name', 'order' => 'ASC',.

8
  • How would I pull all teags including those that aren't attached to a post? Commented Aug 31, 2011 at 18:27
  • Set the 'number' parameter to 0, it'll pull all the tags. This function retrieves general tags, not the ones attached to a post.
    – kevin
    Commented Aug 31, 2011 at 18:44
  • That didn't seem to work. I'm looking to pull absolutely all of the tags. Commented Aug 31, 2011 at 18:56
  • Yes it does. This is the code $args = array('smallest' => 8, 'largest' => 8,'unit' => 'px', 'number' => 0); wp_tag_cloud( $args ); that i used to pull all the tags on this page: regrouped.net/tags-list
    – kevin
    Commented Aug 31, 2011 at 19:02
  • Wild I wonder what's broken then? The only results I'm getting are from tags that are associated with posts. Not any of the ones that are just listed in the Post Tags and have "0". Every result you have listed on that page is connected to a post. Nice site btw. Commented Aug 31, 2011 at 19:12
1

Sorry, but the above answer is lacking a lot of flexibility. wp_tag_cloud() is going to output a whole slew of HTML, and nothing friendly to manipulate in the PHP.

get_tags() already has support for orderby in the input array, since it's a term query (reference: the Codex)

So here's a sample of how you'd do that, and I'm listing the full options below

      $tags = get_tags(array(
        'taxonomy' => 'post_tag',
        'orderby' => 'name',
        'hide_empty' => false // for development
      ));

Big ol' list of all available parameters with their defaults:

$args = array(
        'taxonomy'               => null,
        'object_ids'             => null,
        'orderby'                => 'name',
        'order'                  => 'ASC',
        'hide_empty'             => true,
        'include'                => array(),
        'exclude'                => array(),
        'exclude_tree'           => array(),
        'number'                 => '',
        'offset'                 => '',
        'fields'                 => 'all',
        'count'                  => false,
        'name'                   => '',
        'slug'                   => '',
        'term_taxonomy_id'       => '',
        'hierarchical'           => true,
        'search'                 => '',
        'name__like'             => '',
        'description__like'      => '',
        'pad_counts'             => false,
        'get'                    => '',
        'child_of'               => 0,
        'parent'                 => '',
        'childless'              => false,
        'cache_domain'           => 'core',
        'update_term_meta_cache' => true,
        'meta_query'             => '',
        'meta_key'               => '',
        'meta_value'             => '',
        'meta_type'              => '',
        'meta_compare'           => '',
);

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