How can I get a post’s content by post id? I tried
get_page('ID'); to show content but it does not work.
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
$content = get_post_field('post_content', $post_id); echo do_shortcode( $content );//executing shortcodes
After Pieter Goosen suggestion on
You can use
apply_filters if you wanted the content to be filtered by other plugins. So this eliminates the need to use
$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
If you don’t want shortcode too then just play with the
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 ) );
$page_id = 123; //Page ID $page_data = get_page($page_id); $title = $page_data->post_title; $content = $page_data->post_content;