How to create a playlist for audio associated with posts per category

  1. get a 10 posts from category with slug “gospel”
  2. get post children in loop, filter by mime type to get proper files
  3. add to list

    /**
     * get 10 post from category with SLUG gospel
     */
    $args = array(
        'category_name' => 'gospel',
        'posts_per_page' => 10
    );
    $songs = array();
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            /**
             * get post children, filter by post_mime_type, limit 1
             */
            $args = array(
                'post_parent' => get_the_ID(),
                'post_type'   => 'attachment', 
                'posts_per_page' => 1,
                'post_status' => 'any',
                'post_mime_type' => 'audio'
            );
            $attachments = get_children( $args );
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    $songs[] = wp_get_attachment_url( $attachment->ID);
    
                }
            }
        }
    }
    /**
     * print songs
     */
    print_r($songs);
    /**
     * reset postdata
     */
    wp_reset_postdata();