display wordpress posts by sub category

I am new in wordpress, I am using wordpress Version 4.5, I like to display posts by subcategories, anyone please help how can I do this.
This is what I want

PARENT CATEGORY NAME

Read More

SUB CATEGORY 1

Post 1

Post 2

SUB CATEGORY 2

Post 3

Post 4

SUB CATEGORY 3

Post 5

Post 6

Thanks in advance

Related posts

2 comments

  1. If I am not wrong you need this, you need double loop to fetch the posts under subcategories

    this is how you get current page category

    <?php
        $categories = get_the_category();
        $catID = $categories[0]->cat_ID;
    ?>
    

    and then do this by using above catID

    <?php 
    $subcats = get_categories('child_of=' . $catID);
        foreach($subcats as $subcat) {
            echo '<h3>' . $subcat->cat_name . '</h3>';
            echo '<ul>';
                $subcat_posts = get_posts('cat=' . $subcat->cat_ID);
                foreach($subcat_posts as $subcat_post) {
                    $postID = $subcat_post->ID;
                        echo '<li>';
                            echo '<a href="' . get_permalink($postID) . '">';
                            echo get_the_title($postID);
                            echo '</a>';
                        echo '</li>';
                }
            echo '</ul>';
        } 
    ?>
    
  2. If you want to show all the categories that have subcategories with their respective subcategories that have posts with their respective posts, you can easily do that with the following function, as long as you know the slug of the taxonomy and the post type.

    For example if you have a custom post type called ‘books’ and a custom taxonomy to classify your books called ‘topic’,

    then you would call this function using

    ow_categories_with_subcategories_and_posts( 'topic', 'book' );
    

    That will display a list of all the topics with their respective subcategories followed by the books that belong to each subcategory.

    Something like

    Drama

    • drama subcategory 1

      • book1
      • book2
      • book3
    • drama subcategory 2

      • book4
      • book5

    Comedy

    • comedy subcategory 1

      • book6
      • book7
    • comedy subcategory 2

      • book8
      • book9

    The following is the code for this:

    function ow_categories_with_subcategories_and_posts( $taxonomy, $post_type ) {
    
        // Get the top categories that belong to the provided taxonomy (the ones without parent)
        $categories = get_terms( 
            array(
                'taxonomy'   => $taxonomy,
                'parent'     => 0, // <-- No Parent
                'orderby'    => 'term_id',
                'hide_empty' => true // <!-- change to false to also display empty ones
            )
        );
        ?>
        <div>
            <?php
            // Iterate through all categories to display each individual category
            foreach ( $categories as $category ) {
    
                $cat_name = $category->name;
                $cat_id   = $category->term_id;
                $cat_slug = $category->slug;
    
                // Display the name of each individual category
                echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug  . '</h3>'; 
    
    
                // Get all the subcategories that belong to the current category
                $subcategories = get_terms(
                    array(
                        'taxonomy'   => $taxonomy,
                        'parent'     => $cat_id, // <-- The parent is the current category
                        'orderby'    => 'term_id',
                        'hide_empty' => true
                    )
                );
                ?>
                <div>
                    <?php
    
                    // Iterate through all subcategories to display each individual subcategory
                    foreach ( $subcategories as $subcategory ) {
    
                        $subcat_name = $subcategory->name;
                        $subcat_id   = $subcategory->term_id;
                        $subcat_slug = $subcategory->slug;
    
                        // Display the name of each individual subcategory with ID and Slug
                        echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';
    
                        // Get all posts that belong to this specific subcategory
                        $posts = new WP_Query(
                            array(
                                'post_type'      => $post_type,
                                'posts_per_page' => -1, // <-- Show all posts
                                'hide_empty'     => true,
                                'order'          => 'ASC',
                                'tax_query'      => array(
                                    array(
                                        'taxonomy' => $taxonomy,
                                        'terms'    => $subcat_id,
                                        'field'    => 'id'
                                    )
                                )
                            )
                        );
    
                        // If there are posts available within this subcategory
                        if ( $posts->have_posts() ):
                            ?>
                            <div>
                                <?php
    
                                // As long as there are posts to show
                                while ( $posts->have_posts() ): $posts->the_post();
    
                                    //Show the title of each post with the Post ID
                                    ?>
                                    <p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
                                    <?php
    
                                endwhile;
                                ?>
                            </div>
                            <?php
                        else:
                            echo 'No posts found';
                        endif;
    
                        wp_reset_query();
                    }
                    ?>
                </div>
                <?php
            }
            ?>
        </div>
        <?php
    }
    

    Then if you ONLY want to get the subcategories related to comedy, and you want to provide the ‘comedy’ slug as a filter to get something like:

    • comedy subcategory 1

      • book6
      • book7
    • comedy subcategory 2

      • book8
      • book9

    Then you would call the following function like this:

    ow_subcategories_with_posts_by_category( 'topic', 'book', 'comedy' );
    

    And the function to do that will be:

    function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
        $category = get_term_by( 'slug', $term, $taxonomy );
        $cat_id   = $category->term_id;
    
        // Get all subcategories related to the provided $category ($term)
        $subcategories = get_terms(
            array(
                'taxonomy'   => $taxonomy,
                'parent'     => $cat_id,
                'orderby'    => 'term_id',
                'hide_empty' => true
            )
        );
        ?>
        <div>
            <?php
            // Iterate through all subcategories to display each individual subcategory
            foreach ( $subcategories as $subcategory ) {
    
                $subcat_name = $subcategory->name;
                $subcat_id   = $subcategory->term_id;
                $subcat_slug = $subcategory->slug;
    
                // Display the name of each individual subcategory with ID and Slug
                echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';
    
                // Get all posts that belong to this specific subcategory
                $posts = new WP_Query(
                    array(
                        'post_type'      => $post_type,
                        'posts_per_page' => -1, // <-- Show all posts
                        'hide_empty'     => true,
                        'order'          => 'ASC',
                        'tax_query'      => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'terms'    => $subcat_id,
                                'field'    => 'id'
                            )
                        )
                    )
                );
    
                // If there are posts available within this subcategory
                if ( $posts->have_posts() ):
                    ?>
                    <div>
                        <?php
                        while ( $posts->have_posts() ): $posts->the_post();
    
                            //Show the title of each post with the Post ID
                            ?>
                            <p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
                            <?php
                        endwhile;
                        ?>
                    </div>
                    <?php
                else:
                    echo 'No posts found';
                endif;
    
                wp_reset_query();
            }
            ?>
        </div>
        <?php
    }
    

Comments are closed.