9

How can I get a post's content by post id? I tried get_page('ID'); to show content but it does not work.

  • 1
    Downvotes as you did not even try to read the docs on get_page(). It has been deprecited a very long time ago. Also, there are an unlimited amount of resources on site regarding this issue, even google has tons of info on this – Pieter Goosen Jan 6 '16 at 7:25
15

You can do it multiple ways. Following are best two ways.

$post_id = 5// example post id
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo do_shortcode( $content );//executing shortcodes

Another method

$content = get_post_field('post_content', $post_id);
echo do_shortcode( $content );//executing shortcodes

After Pieter Goosen suggestion on apply_filters.

You can use apply_filters if you wanted the content to be filtered by other plugins. So this eliminates the need to use do_shortcode

Example

$post_id = 5// example post id
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo apply_filters('the_content',$content);
 //no need to use do_shortcode, but content might be filtered by other plugins.

If you don't want to allow other plugins to filter this content and need shortcode function then go with do_shortcode.

If you don't want shortcode too then just play with the post_content.

  • Just wonder why you are using do_shortcode – Pieter Goosen Jan 6 '16 at 7:25
  • Hi thanks for asking. @PieterGoosen As we are getting the raw content of post. Any shortcode embedded in the post won't be processed. so we are doing that by ourself with do_shortcode – WPTC-Troop Jan 6 '16 at 7:36
  • 2
    A better way would be to use apply_filters( 'the_content', $content );, this way, all filters that are applied to the_content() like wpautop and the shortcode handler, is applied to $content. ;-). Note the plural filters – Pieter Goosen Jan 6 '16 at 7:59
  • 1
    Yes, using apply_filters instead of do_shortcode make sense. But using apply_filter is purely based on their environment decision. Let me update my answer too. Thank you so much for your care on community @PieterGoosen – WPTC-Troop Jan 6 '16 at 9:04
0

I'll just leave here another hacky ugly way that you may find useful sometimes. Of course methods which use API calls are always preferred (get_post(), get_the_content(), ...).

global $wpdb;
$post_id = 123; // fill in your desired post ID
$post_content_raw = $wpdb->get_var(
    $wpdb->prepare(
        "select post_content from $wpdb->posts where ID = %d",
        $post_id
    )
);
0
$id = 23; // add the ID of the page where the zero is
$p = get_page($id);
$t = $p->post_title;
echo '<h3>'.apply_filters('post_title', $t).'</h3>'; // the title is here wrapped with h3
echo apply_filters('the_content', $p->post_content);
  • 1
    Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jan 19 '18 at 6:30
-1

By using get_page('ID').

$page_id = 123;  //Page ID
$page_data = get_page($page_id); 
$title = $page_data->post_title; 
$content = $page_data->post_content;
  • 1
    Downvoted as you really did not even tried to read the documentation. get_page() is depreciated – Pieter Goosen Jan 6 '16 at 7:27

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.