A-Z Index of Categories for WordPress

Has anyone seen any plugin for wordpress that provided an alphabetical index of categories that would take you to a page of categories belonging to a particular letter. Basically, there is a list: A B C D … X Y Z that you can click on, and when you click on A, you go to a page where you see all the categories that start with letter A?

I have searched for over an hour and cannot find this… I started messing around with the MySQL doing this SELECT term_id as id, name as post_title FROM wp_terms ORDER BY name but this is also including tags within categories, and I just want to grab the categories.

Read More

Update:

Ok, so it looks like I can grab just the categories from wp_term_taxonomy via something like this:

SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy =  'category'

Then, I can take that array and run it here:

SELECT term_id, name FROM wp_terms WHERE term_id IN (that-array) ORDER BY name

How is that? How to create a new table using what I have above?

Related posts

Leave a Reply

2 comments

  1. You can use this function and pass a letter as a parameter

      function get_category_by_letter($letter){
        $args=array(
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => 0);
    
        $categories=get_categories($args);
        foreach($categories as $category) {
    
        $catname = $category->name;
        $first_letter = substr(strip_tags($catname), 0 , 1); // get the first letter of the category
        if(strcasecmp($first_letter,$letter) != 0) continue; //if not the same letter then loop next NOTE: this is case insensitive comparison
        else{
          $cats[] = $category->term_id; //store category IDs in array
          //$cats[] = $category->name; uncomment this if you want to get category name
          //or you can start your process here and remove the return value  
            }
        }
     return $cats;
    }
    

    Example usage

      $cats = get_category_by_letter('A');
      var_dump($cats);