Get post content from outside the loop

Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.

Related posts

Leave a Reply

11 comments

  1. You can use get_page() to return the $post object of a static page:

    $page_id = 302;
    $page_object = get_page( $page_id );
    echo $page_object->post_content;
    

    Edit

    Similarly, you can use get_post() to return the $post object of a post:

    $post_id = 302;
    $post_object = get_post( $post_id );
    echo $post_object->post_content;
    
  2. to get the content of the post outside the loop you can write something like this

    global $post;
    $content = $post->post_content;
    
    if ( !empty( $content ) ) :
        echo $content;
    endif;
    
  3. If your content include shortcodes, you should use:

    $post_id = 22;        
    $post_object = get_post( $post_id );        
    echo do_shortcode( $post_object->post_content );
    
  4. Was looking for the same thing, I’m surprised nobody said this:

    $pageID = 302;
    
    echo get_the_content(null, false, $pageID);
    

    Works good! 🙂

  5. For completeness, building on Tim’s comment above and inspired by Stephen Harris’s article, the solution that enables use of the_content() is:

    $post_id = 302;
    global $post;
    $post = get_post($post_id);
    setup_postdata( $post );
    the_content();
    wp_reset_postdata( $post );
    

    And hence filters get applied (paragraphs will be inserted etc.) and shortcodes work.

  6. You can use, as said, the solution with get_post and $post_object->post_content, but don’t forget to add a check before you use that post object:

    function get_post_content( $post_id = null ) {
        $post_object = get_post( $post_id );
        if ( ! $post_object ) { return ''; }
        //else
    
        return apply_filters('the_content', $post_object->post_content);
    }
    
    echo get_post_content( $other_post_id );
    
  7. Since you know your target post ID (302), you may find useful this shorthand syntax that you can use out of the loop (though its performance is pretty much the same as in any other alternative method:)

    echo(get_post_field('post_content',302));
    
  8. You can use the get_post_data() function to get post outside the loop. Place this code in functions.php

    function get_post_data($postId) {
        global $wpdb;
        return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId");
    }
    

    and then add this snippet for more control on the process

    <?php $data = get_post_data(302);
        echo $data->post_date;     // post date
        echo $data->post_title;    // post title
        echo $data->post_content;  // post content
        echo $data->comment_count; // comments number
    ?>
    
  9. use wp_reset_postdata(); it will work.. (edited)

    <?php 
    $args = array(
            'post_type' => 'posttype',
            'p' => 'post_id'
           );
    $the_query = new WP_Query( $args );
    if( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php the_content(); ?>
        <?php endwhile; endif; 
    wp_reset_postdata();
    ?>
    

    posttype can be “post” , “page” or your custom post type. Here p=302 is your post id.. Hope it will work.

  10. you can put content in a category X and use query_post before while like this :

        <?php query_posts('cat=X&showposts=1'); ?>
        <?php while (have_posts()) : the_post(); ?>
              <?= get_the_content(); ?>
        <?php endwhile; ?>