The serialized array your meta boxes are saving is deformed. I made a few slight changes to your code to get it to work. I created a new post and here is what was saved after being unserialized.
Note: WordPress will automatically unserialize an an array stored in post_meta. Use an online unserializer if you ever want to check the contents.
Array
(
[1] => Array
(
Custom Meta Boxes: Store two values in one repeatable field => Back in Black
[track] => 1
) [2] => Array
(
Custom Meta Boxes: Store two values in one repeatable field => Hells Bells
[track] => 2
) [3] => Array
(
Custom Meta Boxes: Store two values in one repeatable field => Hot for Teacher
[track] => 3
) [4] => Array
(
Custom Meta Boxes: Store two values in one repeatable field => Rack City Bitch
[track] => 4
)
)
To output this on the front end you just call get_post_meta() and loop through the array.
add_filter( 'the_content', 'get_your_track_meta' );
function get_your_track_meta( $content ) {
global $post;
$metadata = get_post_meta( $post->ID, 'songs', true );
$content .= '<ul>';
foreach ( (array) $metadata as $meta) {
$content .= '<li>' . $meta['title'] . ' -- ' . $meta['track'] . '</li>';
}
$content .= '</ul>';
return $content;
}
On the front end this gives us a nice unordered list with the track and title:
- Back in Black — 1
- Hells Bells — 2
- Hot for Teacher — 3
- Rack City Bitch — 4
Code Changes:
I had to change your page field to post
in you meta box definitions to get it to load on a post and I also moved them inside the add_meta_box function so we wouldn’t have to deal with making them global. In your show box function I cast the get_post_meta variable to an array to get rid of the invalid argument supplied for foreach error I got in Xdebug and fixed your counter.
$songs = get_post_meta( $post->ID, 'songs', true );
$c = 0;
if ( count( $songs ) > 0 ) {
foreach ( (array)$songs as $track ) {
if ( isset( $track['title'] ) || isset( $track['track'] ) ) {
printf( '<p>Song Title <input type="text" name="songs[%1$s]Custom Meta Boxes: Store two values in one repeatable field" value="%2$s" /> -- Track number : <input type="text" name="songs[%1$s][track]" value="%3$s" /><span class="remove">%4$s</span></p>', $c, $track['title'], $track['track'], __( 'Remove Track' ) );
$c++;
}
}
}