Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!!

share|improve this question
Please add what this is about? Plugin? Theme? And where have you added the code? Please also try to use English replacements in your questions so people can easier imagine what it is about. THanks. – kaiser Jan 11 at 16:23

2 Answers

I played a little and this is what I came up with, I tested and it works just like your given example:

<ul>
<?php

// get initial categories
$categories = get_categories();

foreach ( $categories as $category ) {

// we don't want child categories now, and since get_categories does not support 'depth' parameter, we use ->parent check
if ( $category->parent > 0 ) {
    continue;   
}

$i = 0;
echo '<li>' . $category->cat_name . '</li>';

$posts = get_posts(
    array(
        'category' => $category->term_id
    )
);

foreach ( $posts as $post ) {

    // let's make sure that the post is not also in any of child categories, if it is, skip it ( we don't want to display it twice )
    $child_categories = get_term_children( $category->term_id, 'category' );

    if ( $child_categories && in_category( $child_categories, $post->ID ) ) {
        continue;
    }

    echo 0 === $i ? '<ul>' : '';
    echo '<li>' . $post->post_title . '</li>';
    $i++;

}

// now, after we listed all the posts, we query for child categories
$categories2 = get_categories(
    array(
        'parent' => $category->term_id
    )
);

foreach ( $categories2 as $category ) {

    $j = 0;
    echo '<li>' . $category->cat_name . '</li>';
    $posts2 = get_posts(
        array(
            'category' => $category->term_id
        )
    );

    foreach ( $posts2 as $post ) {

        echo 0 === $j ? '<ul>' : '';
        echo '<li>' . $post->post_title . '</li>';
        $j++;
    }

    echo null === $posts2 ? '' : '</ul>';

}

echo null === $posts ? '' : '</ul>';

}

?>
</ul>
share|improve this answer

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>';

}
}

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.