Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Custom Post Type Post Content with Shortcode WordPress

  • SOLVED

I need a solution that allows me to display an individual custom post type by its slug on another post or page.

<strong>Example: </strong>
I have a custom post type named: reports

I create a post in reports and the actual post title and slug is: rep-12345

I want to be able to insert [rep-12345] into any post or page and have it display the content of that custom post type post.

I don't want to list all the posts. I only want to display the content of that unique custom post type.

Thanks

Answers (3)

2013-10-09

Remy answers:

If you want to query by slug
cpt_content_func($atts){

extract( shortcode_atts( array(

'slug' => null,

), $atts ) );

$args = array(
'name' => $slug,
'post_type' => 'reports',
'numberposts' => 1
);

$post = get_posts( $args );

$content = $post[0]->post_content;

return $content;
}

add_shortcode('rep','cpt_content_func');


use it like this [rep slug="rep-12345"].


Dan Nickerson comments:

Remy,

Thanks, that's better and workable. Would still like to get down to just the slug if possible, but this might be the solution. Unfortunately it's breaking my test install, causing unexpected end 600 lines above which is weird.


Dan Nickerson comments:

I've tried 3 themes and they all break in functions.php with unexpected '{' roughly 578-584 lines above the code.


Remy comments:

The tag ('rep' here) is required to created a shortcode, so you can't really do without it.

My code is missing a function before cpt_content_func, corrected code below :
function cpt_content_func($atts){
$post = '';
$content = '';
extract( shortcode_atts( array(
'slug' => null,
), $atts ) );

$args = array(
'name' => $slug,
'post_type' => 'reports',
'numberposts' => 1
);

$post = get_posts( $args );

if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}

add_shortcode('rep','cpt_content_func');


Dan Nickerson comments:

Great, I thought that looked odd, but saw second code submitted with same error. There are plugins like Shortcode UI which let you name your own shortcodes anything. But this will work and awarding to you. Thanks

2013-10-09

Kyle answers:

You need to give the attribute a variable

cpt_content_func($atts){

extract( shortcode_atts( array(
'id' => null,
), $atts ) );

$post = get_post($id);
$content = $post->post_content;
return $content;

}
add_shortcode('rep','cpt_content_func');


So it would be [rep id="1234"]


Dan Nickerson comments:

Thanks, yes I know how to do that. I really need the slug solution. Shortcode creators let you assign any name as the shortcode. I just need to skip a step and use the exact slug title to name that shortcode. Even it just populates a custom field name and the post content populates value on "update". I can have plugin created, just posting here to see if there's a simpler solution.

2013-10-09

Arnav Joy answers:

try this

<?php

cpt_content_func($atts){



extract( shortcode_atts( array(



'slug' => null,



), $atts ) );





global $wpdb;
$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = %s", $slug, 'reports' , 'publish' ));
if ( $post ) {
$post_data = get_post($post_id);
return $post_data->post_content;
}



return ;

}



add_shortcode('rep','cpt_content_func');


use it as [rep slug="rep-12345"]