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
-
CHILD CATEGORY2
CATEGORY 2
CATEGORY 3
- Post
-
Post
-
CHILD CATEGORY4
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:
<?php
$args=array(
'post_type' => 'biblioteka',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'kategoria-pozycji',
'pad_counts' => false
);
$categories=get_categories($args);
foreach ( $categories as $category ) {
if ( $category->parent > 0 ) {
continue;
}
echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
$querystr = "SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
WHERE term_id = (" . $category->cat_ID . ")
AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
AND ID = object_id
AND post_type="biblioteka"
AND post_status="publish"
ORDER BY post_date DESC";
$posts = $wpdb->get_results($querystr, OBJECT);
echo '<ul>';
foreach ( $posts as $post ) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
$categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
foreach ( $categories2 as $category ) {
echo '<h2>' . $category->name . '</h2>';
$posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );
echo '<ul>';
foreach($posts as $post) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
}
}
?>