0

I have created custom post type Music which have two taxonomies Artist and Album. enter image description here

Now I want to display an albums table which contain all albums and display it in a page, and same for artists.

Can any one help ?

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Sep 28, 2022 at 3:38

1 Answer 1

0

I'd love to help you out. You'll probably want to use the get_terms() function and it may look something like this:

$artists = get_terms(array(
    'taxonomy' => 'artist', // Taxonomy name, or array of taxonomy names
    'hide_empty' => false, // Show terms not assigned to anything
    'orderby' => 'name', // Order the artists by the term name
    'order' => 'DESC' // Order them alphabetically from A to Z
));

You can read more about what parameters are accepted here. That code snippet will pull all artists, and then on the page, you can display them in a table like this:

if ( is_wp_error($artists) ) {

    // Do something about the error

} elseif( count($artists) ) {

    // Artists found, open the table
    printf(
        '<table><thead><tr><th colspan="2">%s</th></tr><tr><th>%s</th><th>%s</th></tr></thead><tbody>',
        __('Artists'), 
        __('Name'), 
        __('Count')
    );

    // Loop through each artist, adding their row
    foreach( $artists as $artist ) {
        printf(
            '<tr><td>%s</td><td>%s<td></tr>',
            esc_html($artist->name),
            esc_html($artist->count)
        );
    }

    // Close the table
    echo('</tbody></table>');
} else {

   // No artists found

}

That example will display a table with two columns, one with the artist's name and then the total number of music post they're assigned to. Since each artist in the loop is a WP_Term object, you can access these properties:

object(WP_Term) (11) {
    ["term_id"]=>  //int
    ["name"]=>   //string 
    ["slug"]=>  //string 
    ["term_group"]=>  //int
    ["term_taxonomy_id"]=> //int
    ["taxonomy"]=>   //string
    ["description"]=>    //string
    ["parent"]=> //int
    ["count"]=>  // int
    ["filter"]= //string
    ["meta"]= array(0) {} //an array of meta fields.

I personally love modularity, so if you want to save writing repetitive code, you could do something like this:

// Define all the custom taxonomies you want to get and their nice label
$taxonomies = array(
   'albums' => __('Albums'),
   'artists' => __('Artists')
);

// Loop through them
foreach($taxonomies as $taxonomy => $taxonomy_name) {

    // Get the terms
    $terms = get_terms(array(
        'taxonomy' => $taxonomy, // Taxonomy name, or array of taxonomy names
        'hide_empty' => false, // Show terms not assigned to anything
        'orderby' => 'name', // Order by the term name
        'order' => 'DESC' // Order them alphabetically from A to Z
    ));
    if ( is_wp_error($terms) ) {

        // Do something about the error

    } elseif( count($terms) ) {

        // Terms found, open the table
        printf(
            '<table><thead><tr><th colspan="2">%s</th></tr><tr><th>%s</th><th>%s</th></tr></thead><tbody>',
            $taxonomy_name, // The nice label
            __('Name'), 
            __('Count')
        );

        // Loop through each term, adding their row
        foreach( $terms as $term ) {
            printf(
                '<tr><td>%s</td><td>%s<td></tr>',
                esc_html($term->name),
                esc_html($term->count)
            );
        }

        // Close the table
        echo('</tbody></table>');
    } else {

       // No terms found
       printf(
            __('No results found for <strong>%s</strong>.'), 
            $taxonomy_name
        );

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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