I'm trying to write a shortcode and am sorely out of practice with this stuff. It's for a plugin Full Width Audio Player and I'm trying to query all tracks within a certain playlist so it will output the track names to add to the player queue.

function kc_tracks ($atts, $content = null) {
            "num" => '',
            "cat" => '44'
    ), $atts));
    global $post;
    $myposts = get_posts('numberposts='.$num.'&order=DESC&post_type=track&orderby=post_date');
    foreach($myposts as $post) :
         $retour.='<li><a href="'.get_post_meta('fap_referral_link').'" class="fap-single-track">'.the_title("","",false).'</a></li>';
    $retour.='</ul> ';
    return $retour;
add_shortcode("tracks", "kc_tracks");

It's this line that I can't get working right.

$retour.='<li><a href="'.get_post_meta('fap_referral_link').'" class="fap-single-track">'.the_title("","",false).'</a></li>';

which outputs this

<a href="" class="fap-single-track dJAX_internal">Downtempo</a>

I've tried a few ways but none worked so ultimately I'm finally asking for help... facepalm moment incoming.

  • this was actually one of the iterations i tried, which has the same result. for extract, what should be used instead? – kristina childs Dec 2 '14 at 18:57

You have a couple of flaws in your code

  • get_post_meta( 'fap_referral_link' ) should be get_post_meta( $post->ID, 'fap_referral_link', true ). Please see get_post_meta()

  • You should always first check if you actually have posts before you run your foreach loop

  • Never ever use extract(). It uses eval() which stand for EVIL. This has been completely removed from Wordpress core years ago due to a specific reason. It poses a big security thread. Please consult the Shortcode API for correct use. Here is an example straight from the codex on how to use a shortcode

    // [bartag foo="foo-value"]
    function bartag_func( $atts ) {
        $a = shortcode_atts( array(
            'foo' => 'something',
            'bar' => 'something else',
        ), $atts );
        return "foo = {$a['foo']}";
    add_shortcode( 'bartag', 'bartag_func' );

Just a couple of extra notes

  • For some reason I can't see why you would need the global $post. It seems totally unnecessary from looking at your code

  • Also, you have cat in your attributes that does not get used.


It seems that your custom field holds more than one value. The best way forward here will be to do a var_dump() of your custom field to see what data is returned and what the exact format is of it.

Check this post I have recently done on custom fields

| improve this answer | |
  • this is actually what i tried first, which returns this <a href="Array" class="fap-single-track dJAX_internal">Downtempo</a>. for the extract... i had swiped this bit of code from smashing magazing, which is usually spot on. but i'll change it. – kristina childs Dec 2 '14 at 19:20
  • The url of the mp3. The way it's supposed to work, when you click on the track (mp3) link, it plays in the audio player. Ultimately there will be two links, one to play it immediately and another to add to the queue. See it in action here: new.kristinachilds.com/music/commercial-wedding – kristina childs Dec 2 '14 at 19:26
  • Also, the cat will be used in the shortcode so I can change it per-use. The cat number calls which playlist to be written client-side. – kristina childs Dec 2 '14 at 19:28
  • 1
    The value of the field is not the URL, it might contain it, but from what you've shown, it is not a string, it's an array. We will need to see the contents of that array if we're to continue – Tom J Nowell Dec 2 '14 at 19:35
  • no, i'm an idiot. it was the wrong meta key, the correct one is fap_track_url which makes that code work. i KNEW it was going to be a facepalm moment... um... is there a way to delete questions cause now i feel silly. and tired. and silly again. – kristina childs Dec 2 '14 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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