Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type called albums and a custom field therein called tracklist which is an array containing song_title and duration. I would like to print a list of the songs on each album.

It's helpful for me to think of the array hierarchy like so:

albumlist
  album
    tracklist
      track
        song_title
        duration

So far I figured out how to drill down to the tracklist array, but not how to display the values of the song titles within it.

$albums = get_posts(array(
  'post_type' => 'album',
  'meta_key'  => 'tracklist'
));

if($albums) {
  foreach($albums as $album) {
    echo get_the_title($album->ID) . "<br>"; // Displays album's title
    $tracklist = get_post_custom_values('tracklist', $album->ID);

    foreach($tracklist as $track) {
      echo $track . "<br>"; // Displays "12" (there are 12 tracks on this album)
    }
  }
}

Anyway, I've been working at this for a couple hours trying to solve it myself, but it's getting late! Once I get to the tracklist array, how do I drill inside the track and get the song title?

Thanks for any help you can proffer.

share|improve this question
1  
get_post_custom_values gets all the custom fields for a post, if you want individual custom field, use get_post_meta instead. See my answer below. –  Rutwick Gangurde Dec 20 '11 at 4:10
1  
@RutwickGangurde: I think get_post_custom_values return all the values of a custom field. Not like that it returns all the custom fields of a post. Refer to codex.wordpress.org/Function_Reference/get_post_custom_values –  booota Dec 20 '11 at 4:55
    
Yes but the user is only wanting one particular field(tracklist), that field being an array doesn't change anything, get_post_meta would be the better choice here. If the asker could print_r() the meta data he gets, it would be alot easier for everyone to advise him or her on how to loop over that data(we need to see the structure of that data). –  t31os Dec 20 '11 at 11:38
    
@booota Ohh my bad... I got it confused with get_post_custom! –  Rutwick Gangurde Dec 20 '11 at 12:52
    
@RutwickGangurde: my pleasure to remind you... hehe :-) –  booota Dec 20 '11 at 16:15
add comment

2 Answers

up vote 1 down vote accepted

If you change

foreach($tracklist as $track) {
  echo $track . "<br>"; // Displays "12" (there are 12 tracks on this album)
}

to

foreach($tracklist as $key => $value ) {
    echo "$key  => $value ('tracklist')<br />"; // Displays "12" (there are 12 tracks on this album)
}

then what you get?

share|improve this answer
    
This is correct, you can simply echo $value though, if you don't want the array info (useful for debugging). –  Wyck Dec 20 '11 at 6:25
    
@Wyck: yeah right. –  booota Dec 20 '11 at 6:34
    
OK, that seemed to work. Also, a great thing I learned was using var_dump($tracklist); really useful to be able to see what you're working with. –  sea_monster Dec 21 '11 at 1:09
add comment

Try this...

$albums = get_posts(array(
        'post_type' => 'album',
        'meta_key'  => 'tracklist'
));

if(isset($albums) && !empty($albums))
{
    foreach($albums as $album)
    {
        echo get_the_title($album->ID) . "<br>"; // Displays album's title
        $tracklist = get_post_meta($album->ID, 'tracklist', true);

        if(isset($tracklist) && !empty($tracklist))
        {
            foreach($tracklist as $track)
            {
                 echo $track . "<br>"; // Displays "12" (there are 12 tracks on this album)
            }
        }
        else
        {
            echo "No Tracklist Added";
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.