Search code examples
wordpresscustom-post-typecustom-taxonomy

How can I echo certain content for each custom taxonomy?


I have a page where I want to display repair prices for various smartphones. I created the custom post type "Reparaturpreise" (repair prices) and also the custom taxonomy "Marken" (brands). For every brand I would like to display the brand name as a heading and below there should be a table which lists various repair prices.

This is the code I have so far:

<?php
               $args = array(
                           'taxonomy' => 'marke',
                           'orderby' => 'name',
                           'order'   => 'ASC'
                       );

               $cats = get_categories($args);

               foreach($cats as $cat) {
            ?>
            
            <h2><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?></h2>
                  
            <?php 
                    $args = array(
                        'post_type' => 'reparaturpreise',
                        'category'  => 'apple',
                        'posts_per_page' => 999
                    );
                    
                    $the_query = new WP_Query( $args ); 
                ?>
                
                    <table class="repair-prices-list">
                        <tr>
                            <th>Gerät</th>
                            <th>Display</th>
                            <th>Akku</th>
                            <th>Backcover</th>
                        </tr>

                        <?php
                            if ( $the_query->have_posts() ) : 
                            while ( $the_query->have_posts() ) : $the_query->the_post();
                        ?>

                        <tr>
                            <td><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?> <?php the_title(); ?></td>
                            <td>€ <?php echo get_post_meta($post->ID, 'displayreparatur', true); ?>,-</td>
                            <td>€ <?php echo get_post_meta($post->ID, 'akkutausch', true); ?>,-</td>
                            <td>€ <?php echo get_post_meta($post->ID, 'backcover-reparatur', true); ?>,-</td>
                        </tr>


                                <?php
                                    wp_reset_postdata();
                                    endwhile;
                                    endif;
                                ?>

                    </table>
            <?php
               }
            ?>

Right now it shows 3 tables, but all with the same heading (first brand) and every table shows all of the phones, not filtered by brand.

You can see the current output here: https://relaunch.websolute.at/reparaturpreise/

Thank you in advance for your help and sorry if my english is not the best.


Solution

  • I managed to work it out myself, this is the code I used:

    <?php
                
                $custom_terms = get_terms('marke');
    
                foreach($custom_terms as $custom_term) {
                    wp_reset_query();
                    $args = array('post_type' => 'reparaturpreise',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'marke',
                                'field' => 'slug',
                                'terms' => $custom_term->slug,
                            ),
                        ),
                     );
    
                     $loop = new WP_Query($args);
                     if($loop->have_posts()) {
                        echo '<h2>'.$custom_term->name.'</h2>'; ?>
    
                        <table class="repair-prices-list">
                            <tr>
                                <th>Gerät</th>
                                <th>Display</th>
                                <th>Akku</th>
                                <th>Backcover</th>
                            </tr>
                                        
                        <?php
                        
                        while($loop->have_posts()) : $loop->the_post(); ?>
                            <tr>
                                <td><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?> <?php the_title(); ?></td>
                                <td>€ <?php echo get_post_meta($post->ID, 'displayreparatur', true); ?>,-</td>
                                <td>€ <?php echo get_post_meta($post->ID, 'akkutausch', true); ?>,-</td>
                                <td>€ <?php echo get_post_meta($post->ID, 'backcover-reparatur', true); ?>,-</td>
                            </tr>
                            <?php
                        endwhile;
                         
                        ?>
                        
                        </table>
                
                <?php
                            
                     }
                }
                
                ?>