get all categories then display all posts in each term

I created custom post and taxonomy. Now I want display post under category like below format.

    taxonomy name 2
  • post 1
  • post 2
  • post 3
  • post all
    taxonomy name 4
  • post 1
  • post 2
  • post 3
  • post all

And continue like that.
I found a code but that doesn’t work.

Code

<?php  
$taxonomy = 'category';   
$param_type = 'category__in';
$term_args=array(
    'orderby' => 'name',  
    'order' => 'ASC');
$terms = get_terms($taxonomy,$term_args);
if ($terms) { 
    foreach( $terms as $term ) {
        $args=array(
            "$param_type" => array($term->term_id),
            'post_type' => 'post', 
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1 
        );
        $my_query = null; 
        $my_query = new WP_Query($args);    
        if( $my_query->have_posts() ) {  ?> 
            <div class="category section">  
            <h3><?php echo 'Category '.$term->name;?></h3>  
            <ul><?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>  
                        <li><a href="<?php the_permalink() ?>" rel="bookmark" 
    title="Permanent Link to <?php the_title_attribute(); ?>">
                        <?php the_title(); ?></a></li>
                    <?php endwhile; ?>
            </ul>
        </div>
    <?php} 
}}

wp_reset_query();  // Restore global post data stomped by the_post().?>

so pls help me. how to fix it.
Thanks

Related posts

2 comments

  1. I’m assuming you are trying to fetch any posts from Taxonomy Category.

    <?php
    $cat_terms = get_terms(
                    array('category'),
                    array(
                            'hide_empty'    => false,
                            'orderby'       => 'name',
                            'order'         => 'ASC',
                            'number'        => 6 //specify yours
                        )
                );
    
    if( $cat_terms ) :
    
        foreach( $cat_terms as $term ) :
    
            //var_dump( $term );
            echo '<h3>'. $term->name .'</h3>';
    
            $args = array(
                    'post_type'             => 'post',
                    'posts_per_page'        => 10 //specify yours
                    'post_status'           => 'publish',
                    'tax_query'             => array(
                                                array(
                                                    'taxonomy' => 'category',
                                                    'field'    => 'slug',
                                                    'terms'    => $term->slug,
                                                ),
                                            ),
                    'ignore_sticky_posts'   => true //caller_get_posts is deprecated since 3.1
                );
            $_posts = new WP_Query( $args );
    
            if( $_posts->have_posts() ) :
                while( $_posts->have_posts() ) : $_posts->the_post();
    
                    echo '<h3>'. get_the_title() .'</h3>';
    
                endwhile;
            endif;
            wp_reset_postdata(); //important
    
    
        endforeach;
    
    endif;
    

    Please note that the caller_get_posts parameter is deprecated since 3.1. Always consult Codex for the latest code directions, and don’t use deprecated code.

    WP_Query() – WordPress Codex

  2. You can try this, you might want to move it inside your template instead of having it inside a function, make sure $tax is correct taxonomy, also change the post_type if you are not using native wordpress Post

    function get_taxonomy_and_post() {
        $tax = 'category'; // Your Taxonomy, change it if you not using wordpress native category
        $terms = get_terms( $tax ,array( // get all taxonomy terms
            'orderby'    => 'name',
            'order'      => 'ASC',
            'hide_empty' => 0,
        ));
    
        //Loop throug each taxonomy terms, 
        foreach ( $terms as $term ) {
    
            //Query argument for post
            $args = array( 
                'post_type' => 'post', // Or Custom Post Type, 
                'order' => 'DESC', 
                'orderby' => 'date',
                'taxonomy' => $tax,
                'term' => $term->slug, // Query posts for each term based on term slug
            );
            $query = new WP_Query( $args ); 
            $posts = $query->get_posts();  
            echo '<div class="category section"><h3>Category '.$term->name.'</h3><ul>';
            if ( $posts ) {
                foreach ( $posts as $post ) {
                    echo '<li><a href="'.$post->guid /**use get_permalink( $post->ID ) if you want the custom permalink**/.'">'.$post->post_title.'</a><li>';
                }
            }
            echo '</ul></div>';
    
        }
    }
    add_action('wp_head', 'get_taxonomy_and_post');
    

Comments are closed.