0

I'm using below code to call the_content of a custom post type post, but when I'm run it, it always getting the current page content not the page whose ID specified. It literally duplicate my page whereever I use it. But it work perfectly when Used Via shortcode inside a post:

$id=12; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content;  

Getting this error:

Attempt to read property "post_content" on null
3
  • Is 12 definitely a post? Is $id always 12? If not, where are you getting $id? Aug 25 '21 at 12:10
  • I'm using this is a widget, which fetch id of post using dropdown, but that works fine.
    – Ajay Malik
    Aug 25 '21 at 12:15
  • Are you sure? You should echo the ID to make sure it’s correct. The error you’re receiving only makes sense if $id is incorrect. Aug 25 '21 at 13:19
0

Set postdata after get_post to set their data.

$id=12; 
$post = get_post($id); 
setup_postdata($post);
$content = apply_filters('the_content', $post->post_content); 
echo $content; 


wp_reset_postdata();

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.