I'm trying to create a plugin that display the specific post content by using POST ID.


[post id="2"]

But I don't know how to send POST ID from function to $my_postid below are my code

function post_function($atts) {
   'id' => 21504,
   ), $atts));

 //getting post content
 $my_postid = 21504;//This is page id or post id

$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);

return $content;
add_shortcode('post', 'post_function');`
  • The way extract() works, you should be able to use it via $id (because that is the key of the array). However, this is usually not recommended. The official docu uses an array as return element, and then access it via that array
    – kero
    Jul 6, 2018 at 14:28
  • @kero it might be worth adding your comment with a bit more detail for an answer so this question doesn't go unanswered. OP you can also answer your own question so that it shows up as Answered.
    – phatskat
    Jul 6, 2018 at 15:17

1 Answer 1


First up, I'd suggest not using extract(). It makes it harder to tell exactly what the available variables are and how they're set, which is part of the confusion here. Instead, just use $atts directly. When you do that, any attributes you pass can be accessed on the $atts array like this:

$id = $atts['id'];

Use shortcode_atts() to ensure that the accepted attributes have default values so that you don't get an error if the user doesn't set the id on the shortcode.

When you use that approach your shortcode would look like this:

function post_function( $atts ) {
    // Set defaults for attributes.
    $atts = shortcode_atts( array(
        'id' => 21504,
    ), $atts );

    // Get the ID attribute.
    $post_id = $atts['id'];

    // Get post by ID.
    $post = get_post( $post_id );

    // Check there is a post with that ID. If so, return the content, with filters applied.
    if ( $post ) {
        $content = $post->post_content;

        return apply_filters( 'the_content', $content );
add_shortcode( 'post', 'post_function' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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