How to search titles/artist/album within a playlist, and only lists the results that match?
I am creating a simple audio WordPress theme, and have a playlist being rendered in the front page using short-code [playlist ids="29,30,31,32,33,34]". How to list only the songs with title (or artist) that matches the text inside an input?
After days of research I came up with this method where I unhook the wp_underscore_playlist_templates()
function and create my own version prefix_wp_underscore_playlist_templates()
. Afterwards I use jQuery to check if data.title
contains the search string. This correctly lists only the items that match the query, but it doesn't playback them correctly. If the search filters only 3 results out of 6, and you click in the 3rd filtered result, it will playback the song that it was originally in the 3rd position. Although the un-matched wp-playlist-item
is not being added to the list, somehow still it exists in the memory of playback. I am also a designer, and partly a developer so forgive my approach to this solution.
script type="text/html" id="tmpl-wp-playlist-item"
# if (data.title.toLowerCase().indexOf("SEARCH QUERY HERE") = 0) { #
div class="wp-playlist-item"
a class="wp-playlist-caption" href="{{ data.src }}"
{{ data.index ? ( data.index + '. ' ) : '' }}
span class="wp-playlist-item-title"{{{ data.title }}}/span
# if ( data.artists data.meta.artist ) { #
span class="wp-playlist-item-artist" — {{ data.meta.artist }}/span
# } #
/a
# if ( data.meta.length_formatted ) { #
div class="wp-playlist-item-length"{{ data.meta.length_formatted }}/div
# } #
/div
# } #
/script