The get_post() WordPress function is one of the most important functions within WordPress. It allows you to get the post content or other information regarding the post easily. In this article, we will introduce you to how you can use the get_post() function to display post data in your plugin or theme.

If you would like more information on creating WordPress plugins, see our tutorial series on creating your first WordPress plugin.

Basic usage

<?php get_post( $id, $output, $filter ); ?>

Parameters

$id: This optional parameter allows you to define the ID of the post that you would like to gather information from. By default, it will get the current post.

$output: By default, it will return a WP_Post object. If you would like an associative or numeric array of field values instead, you may define either ARRAY_A or ARRAY_N.

$filter: Here, you can optionally sanitize the post fields. This parameter defaults to raw which will not filter the content at all. Optional values are raw, edit, db, display, attribute or js.

What is returned?

The get_post() function returns a WP_Post object. The variables that can be defined are the following:

  • ID

  • post_author

  • post_name

  • post_type

  • post_title

  • post_date

  • post_date_gmt

  • post_content

  • post_excerpt

  • post_status

  • comment_status

  • ping_status

  • post_password

  • post_parent

  • post_modified

  • post_modified_gmt

  • comment_count

  • menu_order

For example, we could use the get_post() function to get the author of the post and display it in the following code:

<?php
$post_author = get_post();
echo $post_author->post_author;
?>

Resources

You may find more information on the get_post() function on the get_post WordPress Codex page.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!