Title: Sort Posts by Cat Name
Last modified: August 30, 2016

---

# Sort Posts by Cat Name

 *  Resolved [Michael](https://wordpress.org/support/users/aum001/)
 * (@aum001)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/sort-posts-by-cat-name/)
 * I am creating a Dropdown selection box with a list of custom post types with 
   them prefixed by their category name. Each post will only have one category name.
   This is what I have at the moment. How would I sort them by the Category Name?
 *     ```
       echo '<option value="-1">--Select a Product--</option>';
       while ( $query->have_posts() ) : $query->the_post();
       	$id = get_the_ID();
       	echo '<option value="' .$id. '"' .selected( $product, $id ). '>';
       	$category = get_the_terms( $id->ID, 'product_cat' );
       	foreach ( $category as $cat){
       	   echo $cat->name . ' - ';
       	}
       	the_title();
       	echo '</option>';
       endwhile;
       ```
   
 * _**[Moderator note: code fixed. Please wrap code in the backtick character or
   [use the code button](https://codex.wordpress.org/Forum_Welcome#Posting_Code).]**_
 * Any insights into a solution are greatly appreciated.
 * – Michael

Viewing 1 replies (of 1 total)

 *  Thread Starter [Michael](https://wordpress.org/support/users/aum001/)
 * (@aum001)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/sort-posts-by-cat-name/#post-6687502)
 * I was able find a different solution… so I have closed this post.
 * – Michael

Viewing 1 replies (of 1 total)

The topic ‘Sort Posts by Cat Name’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Michael](https://wordpress.org/support/users/aum001/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/sort-posts-by-cat-name/#post-6687502)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
