I think do you mean GROUP Post
by category/taxonomy NOT SORT
.
Here, Is a code to GROUP
by category/taxonomy
$terms = get_terms( 'my_cat_name' );
Here, cat_name
name is the name of taxonomy, When you register it like this: e.g.
register_taxonomy( 'my_cat_name', array( 'custom_post_name' ), $args )
Use it in Query
e.g.:
$args = array(
'post_type' => 'custom_post_name',
'my_cat_name' => $term->slug,
'posts_per_page' => $no_of_posts,
);
Complete code:
$terms = get_terms( 'CUSTOM_TAXONOMY_SLUG' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
$output .= '<ul class="category-list">';
foreach ( $terms as $term ) {
$output .= '<li class="single-cat">';
$output .= ' <h3>' . $term->name . '</h3>'; // Taxonomy/Category Name
$args = array(
'post_type' => 'POST_TYPE_SLUG',
'CUSTOM_TAXONOMY_SLUG' => $term->slug,
'posts_per_page' => -1,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$output .= '<ul class="cat-post-list">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output .=' <li class="cat-single-post">';
$output .=' <h4><a href="'.get_the_permalink().'">' .get_the_title(). '</a></h4>';
$output .=' </li><!-- .cat-single-post -->';
}
$output .= '</ul><!-- .cat-post-list -->';
}
wp_reset_postdata();
$output .= '</li><!-- .single-cat-item -->';
}
$output .= '</ul><!-- .category-list -->';
}