I have an audio website and I want to allow users to make custom playlists from posts.
I am using posts as Albums which contains the Tracks. I thought of creating a custom post type and using it to hold a user select playlist based on the default posts.
Is this a good idea or is there a better way to allow users to create custom playlist.
Notes:
- I had a look at this tutorial. It is what I’m looking for but for individual tracks in the post.
- The audio is stored in arrays in custom fields, i.e
custom_audio{ audio=>"name", url=>"www.text.com"}
1 Answer
What you are looking for is to create a relationship between a Custom Post Type and the Post type.
If your users have editing capability for the CPT, then yes, it’s a good idea to use the CPT’s to hold the information about the Albums (normal posts).
The easiest/fastest solution is to use a plugin.
Posts to Posts is meant exclusively for this (my emphasis):
This plugin allows you to create many-to-many relationships between
posts of any type: post, page, custom etc. A few example use cases:
- manually curated lists of related posts
- post series
- products connected to retailers
- etc.
Additionally, you can create many-to-many
relationships between posts and users. So, you could also implement:
- favorite posts of users
- multiple authors per post
- etc.
And another one is Advanced Custom Fields. As I’m a regular user, I can illustrate its use for this case.
Create a Field Group with the following configuration:
click to enlarge
That results in this:
The result snapshot have a little trick though. The normal box that ACF generates has a very short height.
To modify the box height, use the following code in your theme’s functions.php
file, or a custom plugin which makes this theme independent.
/**
* ADJUST THE VALUE OF 'playlists' TO THE CUSTOM POST TYPE SLUG
* ADJUST THE VALUE OF #acf-album_posts TO MATCH THE FIELD NAME
*/
add_action('admin_head-post.php', 'wpse_73351_admin_head');
add_action('admin_head-post-new.php', 'wpse_73351_admin_head');
function wpse_73351_admin_head()
{
global $post;
// Not our post_type, do nothing
if ( 'playlists' != $post->post_type )
return;
?>
<style>
#acf-album_posts select {
height: 400px !important;
}
</style>
<?php
}
Finally, read the plugin’s documentation to see how to grab the album_posts
field value and play that funky music 🙂