Please excuse my ignorance, this seems simple but I can't figure it out and I've been rushing to hack things together. I can't find anything online to help me out.

What I have: A custom post type named "case_study". There are going to be a lot of these published and they will be used in various places throughout the site, some of them more than once.

What I need: To print an individual case study where needed, by ID. I feel like using a shortcode would be the most obvious way to do this, but I'm failing to get there. (Perhaps there is a better user-friendly option?)

I would like my shortcode to work like this: [case_study id=31]

Here is my plugin code. This currently prints the entire case study category when I use the short code:

//Case study Post Type
add_action('init', 'register_cpt_case_study');

function register_cpt_case_study() {

    $labels = array(
        'name' => _x('Case Studies', 'case_study'),
        'singular_name' => _x('Case Study', 'case_study'),
        'add_new' => _x('Add New', 'case_study'),
        'add_new_item' => _x('Add New Case Study', 'case_study'),
        'edit_item' => _x('Edit Case Study', 'case_study'),
        'new_item' => _x('New Case Study', 'case_study'),
        'view_item' => _x('View Case Study', 'case_study'),
        'search_items' => _x('Search Case Studies', 'case_study'),
        'not_found' => _x('No case studies found', 'case_study'),
        'not_found_in_trash' => _x('No case studies found in Trash', 'case_study'),
        'parent_item_colon' => _x('Parent Case Study:', 'case_study'),
        'menu_name' => _x('Case Studies', 'case_study'),

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'A case study plugin for better management and organization',
        'supports' => array('title', 'editor'),
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_icon' => 'http:www.changethis.com', //change this icon
        'show_in_nav_menus' => false,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'

    register_post_type('case_study', $args);

function case_study_shortcode($atts) {
        'id' => '',
                    ), $atts));
//The Query
//The Loop
    if (have_posts()) : while (have_posts()) : the_post();
            echo '<h3>';
            echo the_title();
            echo '</a></h3>';
            echo the_content();

//Reset Query

add_shortcode('case_study', 'case_study_shortcode');

Any advice would be greatly appreciated!

  • Please take a look at this answer for why to not use query_posts(). – kaiser Mar 6 '14 at 8:25

Ok, I found a simple answer to my problem. This is the shortcode function I ended up using to get my desired result:

function cpt_case_study_func($atts){

extract( shortcode_atts( array(

    'id' => null,

), $atts ) );

    $post = get_post($id);
    $content = '<div class="case-study"><h3>' . $post->post_title . '</h3>   <p>' . $post->post_content . '</p></div>';
    return $content;


Thanks to help from Kyle on this post

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.