Order posts by category name

Is there any way to order posts by category name?

I tried

$args = array(
    'post_type' => 'dlm_download',
    'posts_per_page' => 10,
    'paged' => $paged,
    'order_by'=> 'cat', 
    'order' => 'ASC'
);

But it does not really work, could please someone give me a hint?

2 Answers
2

I had the same struggle like you, thank i sat down and rethought the whole thing, what you actually need to is select all the categories, and pass the category id to it.

Since you use a dlm_download you will need to do the following, get the terms from the dlm_download_category

$cats = get_terms('dlm_download_category');

Loop trough the category and pass query_posts in it

so

foreach ($cats as $cat) 
{

$args = array(
'post_type' => 'dlm_download',
'dlm_download_category' => $cat->slug,
);

query_posts($args);

if (have_posts()) : 
while (have_posts()) : the_post(); 
// your stuff here
endwhile;
endif; 
}

And there you go

PS: if you want to make it with posts not with custom post

Than $cats = get_categories();

and pass 'cat' in to the argument.

Hope its clear and helps. Cheers

Leave a Comment