João Paulo Cercal

Staff Security Engineer at LastPass

  • PHP

WordPress Hack: How to get a Post's content by ID

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn

Read this article in

When you’re building a theme for WordPress, you may need to get the content of a specific post using the ID as a filter. Most of the time you get a post’s content through the get_post_content function inside a Loop.

However, when you need to retrieve a post’s information outside the loop context or at a separate point in the application, the best solution is to use the get_post_field('field-name'), $postId) function.

Right below you can check an example of retrieving a post’s content by ID:

1
2
3
<?php

echo get_post_field('post_content', $post_id);

You can find more details about this function in the official WordPress documentation.