$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display taxonomies for custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display taxonomies for custom post type

matteradmin9PV0评论

I have a custom post type as Products with custom taxonomies product-year product-cat and product-country

I want to create a table like below but I am not sure how to populate the code.

Thank you in advance

I have a custom post type as Products with custom taxonomies product-year product-cat and product-country

I want to create a table like below but I am not sure how to populate the code.

Thank you in advance

Share Improve this question edited Feb 25, 2019 at 6:59 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 25, 2019 at 5:40 t.cvmkt.cvmk 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Here is a quick solution, that may guide you to desired result.

$years = get_terms( array(
        'taxonomy' => 'product-year',
        'hide_empty' => true,
    ) );

    $cats = get_terms( array(
        'taxonomy' => 'product-cat',
        'hide_empty' => true,
    ) );

if ( ! empty( $years ) && ! is_wp_error( $years ) ){
    foreach ( $years as $year ) {
        echo '<strong>'.$year->name.'</strong>';
        if ( ! empty( $cats ) && ! is_wp_error( $cats ) ){
            foreach ( $cats as $cat ) {
                echo '<strong>'.$cat->name.'</strong>';
                $args = array(
                            'post_type' => 'Products',
                            'tax_query' => array(
                                'relation' => 'AND',
                                array(
                                    'taxonomy' => 'product-year',
                                    'field'    => 'term_id',
                                    'terms'    => array( $year->term_id ),
                                    ),
                                array(
                                    'taxonomy' => 'product-cat',
                                    'field'    => 'term_id',
                                    'terms'    => array( $cat->term_id ),
                                ),
                            ),
                        );
                $query = new WP_Query( $args );
                if ( $query->have_posts() ) {
                    echo '<table>';
                    while ( $query->have_posts() ) {
                        $query->the_post();

                        $countries = get_the_terms( get_the_ID(), 'product-country' );

                        if ( $countries && ! is_wp_error( $countries ) ) : 

                              $countries_links = array();

                              foreach ( $countries as $country ) {
                                  $countries_links[] = $country->name;
                               }

                              $countries_names = join( ", ", $countries_links );
                        }
                        echo '<tr><td>' . get_the_title( ) . '</td>'.'<td>' . $countries_names . '</td></tr>'  ;
                    }
                    echo '</table>';

                    // Restore original Post Data
                    wp_reset_postdata();
                }
            }
        }
    }
}

Please take care of any typo if any.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far