Ask your WordPress questions! Pay money and get answers fast! (more info)

Post type inside another post type WordPress

I need help "linking" one post type to another. It's a bit hard to explain.

I've got one post type called 'album'. This contains the title of the album and the track titles entered into a text field with unique names (track1, track2, ...) and a thumbnail.
See mockup here: https://dl.dropboxusercontent.com/u/175241/wp/ui-albums.jpg

Then I've got another post type 'transcription' with more information about each song (text, file attachments, creator information, ...). Inside 'transcription' I need to be able to select which track I want to add information too.
See mockup here: https://dl.dropboxusercontent.com/u/175241/wp/trans-ui.jpg

I want to display a list of the tracks where you can click the tracks that have 'transcription' information.
Mockup: https://dl.dropboxusercontent.com/u/175241/wp/ui-list.jpg

I'm using <em>Advanced Custom Fields</em> and <em>Custom Post Type UI</em> which gives me a dropdown of the albums but I cannot select a track.

How can I do that?

Answers (6)

2014-02-23

Hariprasad Vijayan answers:

Hi,

You need to set tracks as post parent when adding a transcription. then you can solve this issue.


Mads R.H. comments:

Hariprasad -> How can I do that?

I've tried doing it without <em>Advanced Custom Fields</em> and <em>Custom Post Type UI </em> and this is what I've got so far:



if ( ! function_exists('codex_album_init') ) {

function codex_album_init() {
$labels = array(
'name' => 'Albums',
'singular_name' => 'Album',
'add_new' => 'Add New',
'add_new_item' => 'Add New Album',
'edit_item' => 'Edit Album',
'new_item' => 'New Album',
'all_items' => 'All Albums',
'view_item' => 'View Album',
'search_items' => 'Search Albums',
'not_found' => 'No albums found',
'not_found_in_trash' => 'No albums found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Albums'
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'album' ),
'capability_type' => 'post',
'menu_icon' => 'dashicons-list-view',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'thumbnail', 'custom-fields' )
);

register_post_type( 'album', $args );
}
add_action( 'init', 'codex_album_init' );

}



if ( ! function_exists('codex_transcription_init') ) {

function codex_transcription_init() {
$labels = array(
'name' => 'Transcriptions',
'singular_name' => 'Transcription',
'add_new' => 'Add New',
'add_new_item' => 'Add New Transcription',
'edit_item' => 'Edit Transcription',
'new_item' => 'New Transcription',
'all_items' => 'All Transcriptions',
'view_item' => 'View Transcription',
'search_items' => 'Search Transcriptions',
'not_found' => 'No transcriptions found',
'not_found_in_trash' => 'No transcriptions found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Transcriptions'
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'transcription' ),
'capability_type' => 'post',
'menu_icon' => 'dashicons-welcome-write-blog',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'thumbnail', 'custom-fields' )
);

register_post_type( 'transcription', $args );
}
add_action( 'init', 'codex_transcription_init' );

}


Hariprasad Vijayan comments:

Okay. Few clarifications,

1: You are adding album title and tracks to post type "Album" and adding more information for each tracks to "Transcription" post type. Right?

2. Where the album and track title storing when adding a post to "Transcription" post type?

3. Do you want to keep the same structure that mentioned in the screenshots?

Let me know.


Mads R.H. comments:

1. Sounds right! I only add information for some tracks.

2. I don't know :(

3. If you've got another way of solving this, ANY suggestion is welcome. I don't really care how it works, as long as it's easy to add content to the site.




Hariprasad Vijayan comments:

Okay. Real problem is, there is no relationship between the track and transcription. My previous suggestion(Setting parent post) is not possible here, because the tracks are not different posts. So, do the following changes

1. Change the each "Tracks" as posts in albums post type. Also change the album title to custom taxonomy or category. ( Now album title as posts and tracks as custom fields)

2. Add some relation(Parent post or custom field) when adding "transcription" to relate tracks and transcription.

I can do it if you provide details. Please PM

2014-02-23

webGP answers:

Hi!

I think that you should use taxonomy for your structure, so probably you should register post type called track or song, register taxonomy called album and add relationship field to your track post type. This field should point to transcription post type, so you can connect transcription to track. If you have any questions don't hesitate to ask.

Bests
webgp


Mads R.H. comments:

webGP -> Does that mean that only the tracks from one album will show up? I've tried using taxonomy earlier, but all the titles entered were available - what a mess! I could sound like a solution, but how can I add such a "relationship"?


webGP comments:

If you add track and assign it to term from album taxonomy you can display it as tracks lists on taxonomy page. It should work as category page for posts. Relation between tracks and transcriptions is different thing, but if each track has its own transcription, probably it's better to add essentials fields to track post type editor using ACF plugin as Remy said.

2014-02-23

Remy answers:

I think it would be easier to maintain by creating a track post type, replacing your transcription post type with all the data for each added wit ACF, and then you can use the post 2 post plugin to link the tracks to the related album


Mads R.H. comments:

I've looked at the ACF plugin and I really don't understand how to set it up

2014-02-23

Sarbartha Das answers:

I will help you out PM back...


Sarbartha Das comments:

Why are you going so complex way to have what you want. While you can have it simply. You can show all information from a single custom type of post that is album. Show-up all transcription information in it.

2014-02-23

S├ębastien | French WordpressDesigner answers:

that sound like you need this plugin : http://wordpress.org/plugins/posts-to-posts/
More informations here : [[LINK href="https://github.com/scribu/wp-posts-to-posts/wiki"]]https://github.com/scribu/wp-posts-to-posts/wiki[[/LINK]]
and here : [[LINK href="http://scribu.net/wordpress/posts-to-posts/"]]http://scribu.net/wordpress/posts-to-posts/[[/LINK]]

it's a very good plugin, by a very serious wordpress developper.

2014-02-24

Ryan S answers:

I don't know of any plugin that did this but if you don't mind looking into how this things work, I mean relationship between posts and category here's a sample code.


global $theme_domain;

// Portfolio
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolios' ),
'singular_name' => __( 'Portfolio' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Portfolio' ),
'edit_item' => __( 'Edit Portfolio' ),
'new_item' => __( 'Add New Portfolio' ),
'view_item' => __( 'View Portfolio' ),
'search_items' => __( 'Search Portfolio' ),
'not_found' => __( 'No portfolio found' ),
'not_found_in_trash' => __( 'No portfolio found in trash' )
),
'public' => true,
'supports' => array( 'title', 'thumbnail', 'editor' ),
'hierarchical' => true,
'rewrite' => array( "slug" => "portfolio" ),
'menu_position' => 82,
'menu_icon' => get_template_directory_uri() . '/images/icons/portfolio-icon.png'
)
);

// Portfolio Category
$args = array(
'label' => __( 'Portfolio Categories', $theme_domain ),
'labels' => array(
'name' => __( 'Portfolio Categories', $theme_domain ),
'singular_name' => __( 'Portfolio Category', $theme_domain ),
'search_items' => __( 'Search Portfolio Category', $theme_domain ),
'popular_items' => __( 'Popular Portfolio Categories', $theme_domain ),
'all_items' => __( 'All Portfolio Categories', $theme_domain ),
'parent_item' => __( 'Parent Portfolio Category', $theme_domain ),
'edit_item' => __( 'Edit Portfolio Category', $theme_domain ),
'update_item' => __( 'Update Portfolio Category', $theme_domain ),
'add_new_item' => __( 'Add New Portfolio Category', $theme_domain ),
'new_item_name' => __( 'New Portfolio Category', $theme_domain ),
'separate_items_with_commas' => __( 'Separate categories with commas', $theme_domain ),
'add_or_remove_items' => __( 'Add or remove categories', $theme_domain ),
'choose_from_most_used' => __( 'Choose from most used categories', $theme_domain )
),
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'rewrite' => array(
'slug' => 'portfolio-category',
'hierarchical' => true,
),
'query_var' => true
);
register_taxonomy( 'portfolio-category', 'portfolio', $args );



Registering custom taxonomy you are allowed to select which custom post-type or Object-types to use


Hope that helps


Mads R.H. comments:

I would much rather have code than use a plugin, so thank you!

Your code is working just fine, but I don't understand how I can set this up to meet my needs. Is 'Portfolio' to be 'Albums' and taxonomy to be the tracktitles???


Ryan S comments:

Yup sure just to make sure you follow the relationship, like

In category there should one or more link to it/posts link to it, so the question is, is this same in your side? is Album there can be one or more Tracktitles?


Hope that helps