0

I have custom taxonomy, how can I add new element to taxonomy edit page in admin panel. For example, I have album as taxonomy and song as post type, my issue is that I want to show all songs that belong to current album when I edit album. Thanks.

1 Answer 1

0

Check that below code block, hope it will help you. Put it in your plugin or in theme's functions.phpfile and it'll work. I tested it with category taxonomy.

The code block-

// {$taxonomy}_edit_form_fields you should use
add_action( 'album_edit_form_fields', 'the_dramatist_add_custom_fields_to_taxonomy_edit_page', 10, 2 );
/**
 * The function for rendering posts related to the term.
 *
 * @param object $tag      Current taxonomy term object.
 * @param string $taxonomy Current taxonomy slug.
 */
function the_dramatist_add_custom_fields_to_taxonomy_edit_page( $tag, $taxonomy) {
    $songs_query = new WP_Query(
        array(
            'post_type' => 'post',
            'post_per_page' => -1,
            array(
                'taxonomy' => 'album',
                'field' => 'slug',
                'terms' => $taxonomy // slug of the term
            )
        )
    );
    ?>
    <tr class="form-field term-description-wrap">
        <th scope="row"><label for="description">Songs</label></th>
        <td>
            <table>
                <?php
                if( $songs_query->have_posts() ):
                    while( $songs_query->have_posts() ): $songs_query->the_post();
                        ?>
                        <tr>
                            <td>
                                <h3><?php the_title()?></h3>
                                <p><?php the_content()?></p>
                            </td>
                        </tr>
                        <?php
                    endwhile;
                endif;
                ?>
            </table>
        </td>
    </tr>
    <?php
    wp_reset_postdata();
}

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.