I’m new here and my english isn’t perfect so I’m sorry for this 🙂 I’m here because I’m workin on site with post list sorted by category and I have problem with sort out my posts in categories and children categories under them. Now everything is messed up. I would like to sort my custom posts like that:
CATEGORY 1
-
CHILD CATEGORY 1
- Post
- Post
- Post
-
CHILD CATEGORY2
- Post
- Post
- Post
CATEGORY 2
- CHILD CATEGORY3
- Post
- Post
- Post
CATEGORY 3
- Post
-
Post
-
CHILD CATEGORY4
- Post
- Post
- Post
Is it possible with this code:
<?php
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'MY_CUSTOM_TAXONOMY'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ):
echo '<div class="category-header"><h3>'.$category->name.'</h3>';
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'MY_CUSTOM_TAXONOMY')).'</p></div>';
$posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'MY_CUSTOM_POST' ) );
foreach($posts as $post) :
setup_postdata($post);
the_title();
endforeach;
endforeach;
?>
I will be grateful for all help, thank You!!
ok, this is my working solution:
My best solution! (works for any taxonomies,including “category”)
Then you can output:
1) target category(taxonomy) tree, using specific ID
2) All categories/taxonomies
I played a little and this is what I came up with, I tested and it works just like your given example:
Here is the solution that I solved the problems as you experience