Post the content of a specific “Custom Post Type” post within a post using a shortcode

The posts on our music blog usually include multiple tracks of music.

I want to have each individual track be its own entity–a custom post type called “track”–which I want to be able to insert freely into a post where I wish using a shortcode.

Read More

The image below explains pretty well what I am looking to do.
(Link to the page that I took the screenshot from: http://iloveomfg.com/210/don-rimini-whatever-kaptain-cadillac-remix/ )

enter image description here

I want to create a shortcode, [track], which echoes the entire contents of the ‘track’ custom post type which id is specified. For example, if the track id is 34, the shortcode to display it within the post would be: [track id=34] or [track id=”34″].

I have taken a look at all the posts related to this topic on this site, and have tried a few of the solutions, but to no avail, so I decided to go ahead and ask for my problem specifically. Can you tell me exactly what I need to do to achieve this?

Let me know if you need me to clarify anything.

Related posts

Leave a Reply

2 comments

  1. I would suggest creating a new custom taxonomy for relations between a post and its “tracks” or grouping of track posts if you’d like
    that way you can easily create a shortcode that will query all the needed tracks at once using a shortcode instead of calling your shortcode over and over and to order them you can create a custom field in track so in your query you can order by that field so it would be something like this (this assumes that you have a custom taxonomy named “post_tracks” and that all of the posts tracks were added to the same term of that taxonomy, also that you have a custom field to order you tracks named “in_order” :

    function get_tracks($atts, $content = null) {  
            extract(shortcode_atts(array(  
                "post_tracks" => '',
                "tracks" => '',
            ), $atts)); 
    
            //if post_tracks relation term was passed:
            if ($atts['post_tracks'] != ''){
                $tracks = NEW WP_Query(array('post_type' => 'track', 'post_tracks' => $atts['post_tracks'], 'orderby' => 'meta_value', 'meta_key' => 'in_order' ));
                while($tracks->have_posts()){
                    $tracks->the_post();
                    //do whatever you want with each track eg:
                    $out .= '<div class="track">
                        <h3>'.get_the_title($post->ID).'<h3>
                        <div class="track_inner">
                            <div class="track_img">'. get_the_post_thumbnail($post->ID, 'thumbnail').'</div>
                            <div class="track_content">'.apply_filters('the_content',get_the_content()).'<div>
                        </div></div>';
                }
                return $out;
            }
    
            //if its a single track you want:
            $tracks = NEW WP_Query(array('post_type' => 'track','post__in' => array($tracks) ));
                while($tracks->have_posts()){
                    $tracks->the_post();
                    //do whatever you want with each track eg:
                    $out .= '<div class="track">
                        <h3>'.get_the_title($post->ID).'<h3>
                        <div class="track_inner">
                            <div class="track_img">'. get_the_post_thumbnail($post->ID, 'thumbnail').'</div>
                            <div class="track_content">'.apply_filters('the_content',get_the_content()).'<div>
                        </div></div>';
                }
            return $out;
    }  
    
    add_shortcode('tracks','get_tracks');
    

    with this you can simple call your shortcode like this:

    [tracks post_tracks="relation_tern_name"]
    

    or just get your single track on at a time:

     [tracks tracks="track_id"]
    
  2. You have two issues here I think.

    1 – Making a short code. This Nettuts tutorial is excellent at stepping through each of the steps needed. (though you don’t need the second half about adding a tinymce button).

    2 – A way of storing track details. I am guessing you could you use a new post for each track with custom meta with the link to the audio file and any other details you want. Then the track=ID would refer to the ID of the post and you would use the shortcode to grab the ID and then hunt down the post with that ID and then include the meta of that post.

    Hope that helps.