get_post()
概述
get_post() 是 WordPress 核心函数,用于根据文章 ID 或文章对象检索文章数据。它支持多种返回格式和过滤器选项,是处理文章数据的基础工具。
关键要点
- 函数接受三个参数:$post(文章 ID 或对象,默认为 null 以获取当前全局文章)、$output(返回类型,可选 OBJECT、ARRAY_A 或 ARRAY_N)、$filter(过滤器类型,如 'raw'、'edit' 等)。
- 返回类型取决于 $output 参数:成功时返回 WP_Post 对象、关联数组或数字数组,失败时返回 null。
- 函数内部处理了多种输入情况,包括全局文章、WP_Post 实例和对象,并调用 sanitize_post() 和 WP_Post 相关方法进行数据过滤和实例化。
- 广泛用于 WordPress 核心和扩展功能中,涉及文章查询、REST API、媒体处理、模板系统等多个领域。
代码示例
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
$post = $GLOBALS['post'];
}
if ( $post instanceof WP_Post ) {
$_post = $post;
} elseif ( is_object( $post ) ) {
if ( empty( $post->filter ) ) {
$_post = sanitize_post( $post, 'raw' );
$_post = new WP_Post( $_post );
} elseif ( 'raw' === $post->filter ) {
$_post = new WP_Post( $post );
} else {
$_post = WP_Post::get_instance( $post->ID );
}
} else {
$_post = WP_Post::get_instance( $post );
}
if ( ! $_post ) {
return null;
}
$_post = $_post->filter( $filter );
if ( ARRAY_A === $output ) {
return $_post->to_array();
} elseif ( ARRAY_N === $output ) {
return array_values( $_post->to_array() );
}
return $_post;
}注意事项
- 参数 $post 必须作为变量传递,因为它是通过引用传递的。
- 使用 $output 参数可以灵活控制返回的数据结构,便于不同场景下的数据处理。
- 过滤器 $filter 影响数据的处理方式,例如 'raw' 返回原始数据,'edit' 适用于编辑上下文。
- 用户贡献笔记提供了 WP_Post 对象字段参考和使用建议,如避免直接访问对象属性以绕过过滤器。
Retrieves post data given a post ID or post object.
Description
See sanitize_post() for optional $filter values. Also, the parameter $post, must be given as a variable, since it is passed by reference.
Parameters
$postint|WP_Post|nulloptional-
Post ID or post object.
null,false,0and other PHP falsey values return the current global post inside the loop. A numerically valid post ID that points to a non-existent post returnsnull. Defaults to global $post.Default:
null $outputstringoptional-
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.
Default:
OBJECT $filterstringoptional-
Type of filter to apply. Accepts
'raw','edit','db', or'display'. Default'raw'.
Return
WP_Post|array|null Type corresponding to $output on success or null on failure.
When $output is OBJECT, a WP_Post instance is returned.
Source
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
$post = $GLOBALS['post'];
}
if ( $post instanceof WP_Post ) {
$_post = $post;
} elseif ( is_object( $post ) ) {
if ( empty( $post->filter ) ) {
$_post = sanitize_post( $post, 'raw' );
$_post = new WP_Post( $_post );
} elseif ( 'raw' === $post->filter ) {
$_post = new WP_Post( $post );
} else {
$_post = WP_Post::get_instance( $post->ID );
}
} else {
$_post = WP_Post::get_instance( $post );
}
if ( ! $_post ) {
return null;
}
$_post = $_post->filter( $filter );
if ( ARRAY_A === $output ) {
return $_post->to_array();
} elseif ( ARRAY_N === $output ) {
return array_values( $_post->to_array() );
}
return $_post;
}
Related
| Uses | Description |
|---|---|
WP_Post::__construct()wp-includes/class-wp-post.php |
Constructor. |
WP_Post::get_instance()wp-includes/class-wp-post.php |
Retrieve WP_Post instance. |
sanitize_post()wp-includes/post.php |
Sanitizes every post field. |
| Used by | Description |
|---|---|
WP_REST_Attachments_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Prepares attachment links for the request. |
_block_bindings_post_data_get_value()wp-includes/block-bindings/post-data.php |
Gets value for Post Data source. |
is_post_embeddable()wp-includes/post.php |
Determines whether a post is embeddable. |
apply_block_hooks_to_content_from_post_object()wp-includes/blocks.php |
Run the Block Hooks algorithm on a post object’s content. |
apply_block_hooks_to_content()wp-includes/blocks.php |
Runs the hooked blocks algorithm on the given content. |
update_ignored_hooked_blocks_postmeta()wp-includes/blocks.php |
Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content. |
_block_bindings_post_meta_get_value()wp-includes/block-bindings/post-meta.php |
Gets value for Post Meta source. |
inject_ignored_hooked_blocks_metadata_attributes()wp-includes/block-template-utils.php |
Inject ignoredHookedBlocks metadata attributes into a template or template part. |
WP_REST_Font_Faces_Controller::get_parent_font_family_post()wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php |
Get the parent font family, if the ID is valid. |
wp_copy_parent_attachment_properties()wp-admin/includes/image.php |
Copy parent attachment properties to newly cropped image. |
WP_REST_Template_Revisions_Controller::get_parent()wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php |
Gets the parent post, if the template ID is valid. |
_wp_preview_meta_filter()wp-includes/revision.php |
Filters preview post meta retrieval to get values from the autosave. |
_get_comment_reply_id()wp-includes/comment-template.php |
Gets the comment’s reply to ID from the $_GET[‘replytocom’]. |
wp_get_latest_revision_id_and_total_count()wp-includes/revision.php |
Returns the latest revision ID and count of revisions for a post. |
wp_get_post_revisions_url()wp-includes/revision.php |
Returns the url for viewing and potentially restoring revisions of a given post. |
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles()wp-includes/class-wp-theme-json-resolver.php |
Returns the custom post type that contains the user’s origin config for the active theme or an empty array if none are found. |
WP_REST_Menu_Items_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Creates a single nav menu item. |
WP_REST_Menu_Items_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Updates a single nav menu item. |
WP_REST_Menu_Items_Controller::delete_item()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Deletes a single nav menu item. |
WP_REST_Menu_Items_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Prepares a single nav menu item for create or update. |
WP_REST_Global_Styles_Controller::get_post()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Get the post, if the ID is valid. |
WP_REST_Global_Styles_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Prepares a single global styles config for update. |
wp_set_unique_slug_on_create_template_part()wp-includes/theme-templates.php |
Sets a custom slug when creating auto-draft template parts. |
_resolve_template_for_new_post()wp-includes/block-template.php |
Sets the current WP_Query to return auto-draft posts. |
_build_block_template_result_from_post()wp-includes/block-template-utils.php |
Builds a unified template object based a post Object. |
WP_REST_Templates_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Updates a single template. |
WP_REST_Templates_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Creates a single template. |
get_adjacent_image_link()wp-includes/media.php |
Gets the next or previous image link that has the same post parent. |
WP_REST_Posts_Controller::check_password_required()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Overrides the result of the post password check for REST requested posts. |
wp_force_plain_post_permalink()wp-includes/link-template.php |
Determine whether post should always use a plain permalink structure. |
is_post_publicly_viewable()wp-includes/post.php |
Determines whether a post is publicly viewable. |
get_post_parent()wp-includes/post-template.php |
Retrieves the parent post object for the given post. |
wp_after_insert_post()wp-includes/post.php |
Fires actions after a post, its terms and meta data has been saved. |
WP_REST_Attachments_Controller::edit_media_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Applies edits to a media item and creates a new attachment record. |
rest_get_route_for_post()wp-includes/rest-api.php |
Gets the REST API route for a post. |
wp_get_user_request()wp-includes/user.php |
Returns the user request object for the specified request ID. |
WP_REST_Attachments_Controller::insert_attachment()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Inserts the attachment post in the database. Does not update the attachment meta. |
WP_REST_Attachments_Controller::post_process_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Performs post-processing on an attachment. |
get_post_datetime()wp-includes/general-template.php |
Retrieves post published or modified time as a |
wp_ajax_media_create_image_subsizes()wp-admin/includes/ajax-actions.php |
Handles creating missing image sub-sizes for just uploaded images via AJAX. |
WP_Query::generate_postdata()wp-includes/class-wp-query.php |
Generates post data. |
WP_REST_Autosaves_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php |
Creates, updates or deletes an autosave revision. |
WP_REST_Autosaves_Controller::create_post_autosave()wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php |
Creates autosave for the specified post. |
WP_REST_Block_Renderer_Controller::get_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php |
Checks if a given request has access to read blocks. |
WP_REST_Block_Renderer_Controller::get_item()wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php |
Returns block output from block’s registered render_callback. |
WP_REST_Post_Search_Handler::prepare_item()wp-includes/rest-api/search/class-wp-rest-post-search-handler.php |
Prepares the search result for a given post ID. |
WP_REST_Post_Search_Handler::prepare_item_links()wp-includes/rest-api/search/class-wp-rest-post-search-handler.php |
Prepares links for the search result of a given ID. |
has_blocks()wp-includes/blocks.php |
Determines whether a post or content string has blocks. |
has_block()wp-includes/blocks.php |
Determines whether a $post or a string contains a specific block type. |
use_block_editor_for_post()wp-includes/post.php |
Returns whether the post can be edited in the block editor. |
WP_Privacy_Policy_Content::notice()wp-admin/includes/class-wp-privacy-policy-content.php |
Adds a notice with a link to the guide when editing the privacy policy page. |
_wp_privacy_resend_request()wp-admin/includes/privacy-tools.php |
Resend an existing request and return the result. |
WP_Customize_Manager::trash_changeset_post()wp-includes/class-wp-customize-manager.php |
Trashes or deletes a changeset post. |
WP_REST_Posts_Controller::check_template()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks whether the template is valid for the given post. |
WP_REST_Revisions_Controller::get_revision()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php |
Get the revision, if the ID is valid. |
WP_REST_Revisions_Controller::get_parent()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php |
Get the parent post, if the ID is valid. |
WP_REST_Posts_Controller::get_post()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Gets the post, if the ID is valid. |
WP_REST_Comments_Controller::get_comment()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Get the comment, if the ID is valid. |
_wp_delete_customize_changeset_dependent_auto_drafts()wp-includes/nav-menu.php |
Deletes auto-draft posts associated with the supplied changeset. |
WP_Widget_Media_Audio::render_media()wp-includes/widgets/class-wp-widget-media-audio.php |
Render the media on the frontend. |
WP_Widget_Media_Video::render_media()wp-includes/widgets/class-wp-widget-media-video.php |
Render the media on the frontend. |
WP_Widget_Media::display_media_state()wp-includes/widgets/class-wp-widget-media.php |
Filters the default media display states for items in the Media list table. |
WP_Widget_Media::is_attachment_with_mime_type()wp-includes/widgets/class-wp-widget-media.php |
Determine if the supplied attachment is for a valid attachment post with the specified MIME type. |
WP_Widget_Media_Image::render_media()wp-includes/widgets/class-wp-widget-media-image.php |
Render the media on the frontend. |
WP_Customize_Manager::_publish_changeset_values()wp-includes/class-wp-customize-manager.php |
Publishes the values of a changeset. |
WP_Customize_Manager::save_changeset_post()wp-includes/class-wp-customize-manager.php |
Saves the post for the loaded changeset. |
WP_Customize_Manager::import_theme_starter_content()wp-includes/class-wp-customize-manager.php |
Imports theme starter content into the customized state. |
WP_Customize_Manager::get_changeset_post_data()wp-includes/class-wp-customize-manager.php |
Gets the data stored in a changeset post. |
wp_update_custom_css_post()wp-includes/theme.php |
Updates the |
wp_get_custom_css_post()wp-includes/theme.php |
Fetches the |
WP_REST_Attachments_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Creates a single attachment. |
WP_REST_Attachments_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Updates a single attachment. |
WP_REST_Terms_Controller::get_items_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php |
Checks if a request has access to read terms in the specified taxonomy. |
WP_REST_Posts_Controller::check_read_permission()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Checks if a post can be read. |
WP_REST_Posts_Controller::handle_template()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Sets the template for a post. |
WP_REST_Posts_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares a single post for create or update. |
WP_REST_Posts_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Creates a single post. |
WP_REST_Posts_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Updates a single post. |
WP_REST_Posts_Controller::delete_item()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Deletes a single post. |
WP_REST_Comments_Controller::check_read_permission()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Checks if the comment can be read. |
WP_REST_Comments_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Prepares links for the request. |
WP_REST_Comments_Controller::update_item()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Updates a comment. |
WP_REST_Comments_Controller::get_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Checks if a given request has access to read the comment. |
WP_REST_Comments_Controller::create_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Checks if a given request has access to create a comment. |
WP_REST_Comments_Controller::get_items_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Checks if a given request has access to read comments. |
WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()wp-includes/class-wp-customize-nav-menus.php |
Sanitizes post IDs for posts created for nav menu items to be published. |
WP_Customize_Nav_Menus::insert_auto_draft_post()wp-includes/class-wp-customize-nav-menus.php |
Adds a new |
WP_Customize_Nav_Menu_Item_Setting::get_original_title()wp-includes/customize/class-wp-customize-nav-menu-item-setting.php |
Get original title. |
_wp_preview_post_thumbnail_filter()wp-includes/revision.php |
Filters post thumbnail lookup to set the post thumbnail. |
wp_get_canonical_url()wp-includes/link-template.php |
Returns the canonical URL for a post. |
wp_get_attachment_caption()wp-includes/post.php |
Retrieves the caption for an attachment. |
_wp_post_revision_data()wp-includes/revision.php |
Returns a post array ready to be inserted into the posts table as a post revision. |
wp_add_trashed_suffix_to_post_name_for_post()wp-includes/post.php |
Adds a trashed suffix for a given post. |
get_post_embed_url()wp-includes/embed.php |
Retrieves the URL to embed a specific post in an iframe. |
get_post_embed_html()wp-includes/embed.php |
Retrieves the embed code for a specific post. |
get_oembed_response_data()wp-includes/embed.php |
Retrieves the oEmbed response data for a given post. |
WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php |
Prints JavaScript settings for parent window. |
WP_Comment::__isset()wp-includes/class-wp-comment.php |
Determines whether a non-public property is set. |
WP_Comment::__get()wp-includes/class-wp-comment.php |
Magic getter. |
wp_handle_comment_submission()wp-includes/comment.php |
Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form. |
get_preview_post_link()wp-includes/link-template.php |
Retrieves the URL used for the post preview. |
WP_Customize_Nav_Menu_Item_Setting::value()wp-includes/customize/class-wp-customize-nav-menu-item-setting.php |
Get the instance data for a given nav_menu_item setting. |
WP_Customize_Nav_Menus::load_available_items_query()wp-includes/class-wp-customize-nav-menus.php |
Performs the post_type and taxonomy queries for loading available menu items. |
WP_Posts_List_Table::column_title()wp-admin/includes/class-wp-posts-list-table.php |
Handles the title column output. |
WP_Site_Icon::create_attachment_object()wp-admin/includes/class-wp-site-icon.php |
Creates an attachment ‘object’. |
WP_Media_List_Table::column_parent()wp-admin/includes/class-wp-media-list-table.php |
Handles the parent column output. |
wp_attachment_is()wp-includes/post.php |
Verifies an attachment is of a given type. |
WP_Query::setup_postdata()wp-includes/class-wp-query.php |
Sets up global post data. |
wp_ajax_parse_embed()wp-admin/includes/ajax-actions.php |
Applies Ajax handlers to a string. |
wp_ajax_parse_media_shortcode()wp-admin/includes/ajax-actions.php |
|
File_Upload_Upgrader::__construct()wp-admin/includes/class-file-upload-upgrader.php |
Construct the upgrader for a form. |
WP_Screen::get()wp-admin/includes/class-wp-screen.php |
Fetches a screen object. |
wxr_post_taxonomy()wp-admin/includes/export.php |
Outputs list of taxonomy terms, in XML tag format, associated with a post. |
get_post_to_edit()wp-admin/includes/deprecated.php |
Gets an existing post and format it for editing. |
WP_List_Table::comments_bubble()wp-admin/includes/class-wp-list-table.php |
Displays a comment count bubble. |
stream_preview_image()wp-admin/includes/image-edit.php |
Streams image in post to browser, along with enqueued changes in |
wp_save_image()wp-admin/includes/image-edit.php |
Saves image to post, along with enqueued changes in |
wp_generate_attachment_metadata()wp-admin/includes/image.php |
Generates attachment meta data and create image sub-sizes for images. |
wp_dashboard_quick_press()wp-admin/includes/dashboard.php |
Displays the Quick Draft widget. |
the_post_password()wp-admin/includes/template.php |
Displays the post password. |
meta_form()wp-admin/includes/template.php |
Prints the form in the Custom Fields meta box. |
touch_time()wp-admin/includes/template.php |
Prints out HTML form date elements for editing post or comment publish date. |
parent_dropdown()wp-admin/includes/template.php |
Prints out option HTML elements for the page parents drop-down. |
wp_popular_terms_checklist()wp-admin/includes/template.php |
Retrieves a list of the most popular terms from the specified taxonomy. |
attachment_submitbox_metadata()wp-admin/includes/media.php |
Displays non-editable attachment metadata in the publish meta box. |
image_media_send_to_editor()wp-admin/includes/media.php |
Retrieves the media element HTML to send to the editor. |
get_attachment_fields_to_edit()wp-admin/includes/media.php |
Retrieves the attachment fields to edit form fields. |
get_media_items()wp-admin/includes/media.php |
Retrieves HTML for media items of post gallery. |
get_media_item()wp-admin/includes/media.php |
Retrieves HTML form for modifying the image attachment. |
get_compat_media_markup()wp-admin/includes/media.php |
|
media_upload_form_handler()wp-admin/includes/media.php |
Handles form submissions for the legacy media uploader. |
media_handle_upload()wp-admin/includes/media.php |
Saves a file submitted from a POST request and create an attachment post for it. |
media_handle_sideload()wp-admin/includes/media.php |
Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload() . |
media_buttons()wp-admin/includes/media.php |
Adds the media button to the editor. |
get_sample_permalink_html()wp-admin/includes/post.php |
Returns the HTML of the sample permalink slug editor. |
_wp_post_thumbnail_html()wp-admin/includes/post.php |
Returns HTML for the post thumbnail meta box. |
wp_check_post_lock()wp-admin/includes/post.php |
Determines whether the post is currently being edited by another user. |
wp_set_post_lock()wp-admin/includes/post.php |
Marks the post as currently being edited by the current user. |
_admin_notice_post_locked()wp-admin/includes/post.php |
Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
wp_create_post_autosave()wp-admin/includes/post.php |
Creates autosave data for the specified post from |
post_preview()wp-admin/includes/post.php |
Saves a draft or manually autosaves for the purpose of showing a post preview. |
wp_autosave()wp-admin/includes/post.php |
Saves a post submitted with XHR. |
_fix_attachment_links()wp-admin/includes/post.php |
Replaces hrefs of attachment anchors with up-to-date permalinks. |
get_sample_permalink()wp-admin/includes/post.php |
Returns a sample permalink based on the post name. |
edit_post()wp-admin/includes/post.php |
Updates an existing post with values provided in |
bulk_edit_posts()wp-admin/includes/post.php |
Processes the post data for the bulk editing of posts. |
get_default_post_to_edit()wp-admin/includes/post.php |
Returns default post information to use when populating the “Write Post” form. |
wp_ajax_save_attachment_order()wp-admin/includes/ajax-actions.php |
Handles saving the attachment order via AJAX. |
wp_ajax_send_attachment_to_editor()wp-admin/includes/ajax-actions.php |
Handles sending an attachment to the editor via AJAX. |
wp_ajax_send_link_to_editor()wp-admin/includes/ajax-actions.php |
Handles sending a link to the editor via AJAX. |
wp_ajax_get_revision_diffs()wp-admin/includes/ajax-actions.php |
Handles getting revision diffs via AJAX. |
wp_ajax_wp_fullscreen_save_post()wp-admin/includes/ajax-actions.php |
Handles saving posts from the fullscreen editor via AJAX. |
wp_ajax_wp_remove_post_lock()wp-admin/includes/ajax-actions.php |
Handles removing a post lock via AJAX. |
wp_ajax_get_attachment()wp-admin/includes/ajax-actions.php |
Handles getting an attachment via AJAX. |
wp_ajax_save_attachment()wp-admin/includes/ajax-actions.php |
Handles updating attachment attributes via AJAX. |
wp_ajax_save_attachment_compat()wp-admin/includes/ajax-actions.php |
Handles saving backward compatible attachment attributes via AJAX. |
wp_ajax_add_menu_item()wp-admin/includes/ajax-actions.php |
Handles adding a menu item via AJAX. |
wp_ajax_add_meta()wp-admin/includes/ajax-actions.php |
Handles adding meta via AJAX. |
wp_ajax_inline_save()wp-admin/includes/ajax-actions.php |
Handles Quick Edit saving a post from a list table via AJAX. |
wp_ajax_delete_post()wp-admin/includes/ajax-actions.php |
Handles deleting a post via AJAX. |
wp_ajax_trash_post()wp-admin/includes/ajax-actions.php |
Handles sending a post to the Trash via AJAX. |
wp_ajax_delete_page()wp-admin/includes/ajax-actions.php |
Handles deleting a page via AJAX. |
wp_ajax_replyto_comment()wp-admin/includes/ajax-actions.php |
Handles replying to a comment via AJAX. |
wp_get_revision_ui_diff()wp-admin/includes/revision.php |
Get the revision UI diff. |
wp_prepare_revisions_for_js()wp-admin/includes/revision.php |
Prepare revisions for JavaScript. |
WP_Comments_List_Table::column_response()wp-admin/includes/class-wp-comments-list-table.php |
|
WP_Comments_List_Table::single_row()wp-admin/includes/class-wp-comments-list-table.php |
|
Walker_Nav_Menu_Edit::start_el()wp-admin/includes/class-walker-nav-menu-edit.php |
Start the element output. |
_wp_ajax_menu_quick_search()wp-admin/includes/nav-menu.php |
Prints the appropriate response to a menu quick search. |
wp_nav_menu_item_post_type_meta_box()wp-admin/includes/nav-menu.php |
Displays a meta box for a post type menu item. |
WP_Posts_List_Table::single_row()wp-admin/includes/class-wp-posts-list-table.php |
|
WP_Posts_List_Table::_page_rows()wp-admin/includes/class-wp-posts-list-table.php |
Displays the nested hierarchy of sub-pages together with paging support, based on a top level page ID. |
Custom_Image_Header::create_attachment_object()wp-admin/includes/class-custom-image-header.php |
Creates an attachment ‘object’. |
author_can()wp-includes/capabilities.php |
Returns whether the author of the supplied post has the specified capability. |
map_meta_cap()wp-includes/capabilities.php |
Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked. |
WP_Customize_Manager::save()wp-includes/class-wp-customize-manager.php |
Handles customize_save WP Ajax request to save/update a changeset. |
has_term()wp-includes/category-template.php |
Checks if the current post has any of given terms. |
get_the_terms()wp-includes/category-template.php |
Retrieves the terms of the taxonomy that are attached to the post. |
wp_trim_excerpt()wp-includes/formatting.php |
Generates an excerpt from the content, if needed. |
wp_notify_postauthor()wp-includes/pluggable.php |
Notifies an author (and/or others) of a comment/trackback/pingback on a post. |
wp_notify_moderator()wp-includes/pluggable.php |
Notifies the moderator of the site about a new comment that is awaiting approval. |
get_the_modified_date()wp-includes/general-template.php |
Retrieves the date on which the post was last modified. |
get_the_time()wp-includes/general-template.php |
Retrieves the time of the post. |
get_post_time()wp-includes/general-template.php |
Retrieves the localized time of the post. |
get_the_modified_time()wp-includes/general-template.php |
Retrieves the time at which the post was last modified. |
get_post_modified_time()wp-includes/general-template.php |
Retrieves the time at which the post was last modified. |
the_weekday()wp-includes/general-template.php |
Displays the localized weekday for the post. |
the_weekday_date()wp-includes/general-template.php |
Displays the localized weekday for the post. |
the_date_xml()wp-includes/general-template.php |
Outputs the date in iso8601 format for xml files. |
get_the_date()wp-includes/general-template.php |
Retrieves the date of the post. |
wp_get_single_post()wp-includes/deprecated.php |
Retrieve a single post, based on post ID. |
get_parent_post_rel_link()wp-includes/deprecated.php |
Get parent post relational link. |
get_the_attachment_link()wp-includes/deprecated.php |
Retrieve HTML content of attachment image with link. |
get_attachment_icon_src()wp-includes/deprecated.php |
Retrieve icon URL and Path. |
get_attachment_icon()wp-includes/deprecated.php |
Retrieve HTML content of icon attachment image element. |
get_attachment_innerHTML()wp-includes/deprecated.php |
Retrieve HTML content of image element. |
user_can_edit_post()wp-includes/deprecated.php |
Whether user can edit a post. |
get_postdata()wp-includes/deprecated.php |
Retrieves all post data for a given post. |
start_wp()wp-includes/deprecated.php |
Sets up the WordPress Loop. |
WP_Query::get_queried_object()wp-includes/class-wp-query.php |
Retrieves the currently queried object. |
WP_Query::the_post()wp-includes/class-wp-query.php |
Sets up the current post. |
WP_Query::get_posts()wp-includes/class-wp-query.php |
Retrieves an array of posts based on query variables. |
wp_scheduled_delete()wp-includes/functions.php |
Permanently deletes comments or posts of any type that have held a status of ‘trash’ for the number of days defined in EMPTY_TRASH_DAYS. |
do_enclose()wp-includes/functions.php |
Checks content for video and audio links to add as enclosures. |
WP_Embed::cache_oembed()wp-includes/class-wp-embed.php |
Triggers a caching of all oEmbed results. |
WP_Embed::maybe_run_ajax_cache()wp-includes/class-wp-embed.php |
If a post/page was saved, then output JavaScript to make an Ajax request that will call WP_Embed::cache_oembed(). |
WP_Embed::shortcode()wp-includes/class-wp-embed.php |
The do_shortcode() callback function. |
get_the_taxonomies()wp-includes/taxonomy.php |
Retrieves all taxonomies associated with a post. |
get_post_taxonomies()wp-includes/taxonomy.php |
Retrieves all taxonomy names for the given post. |
wp_get_shortlink()wp-includes/link-template.php |
Returns a shortlink for a post, page, attachment, or site. |
the_shortlink()wp-includes/link-template.php |
Displays the shortlink for a post. |
get_adjacent_post_link()wp-includes/link-template.php |
Retrieves the adjacent post link. |
get_adjacent_post()wp-includes/link-template.php |
Retrieves the adjacent post. |
get_adjacent_post_rel_link()wp-includes/link-template.php |
Retrieves the adjacent post relational link. |
get_boundary_post()wp-includes/link-template.php |
Retrieves the boundary post. |
get_edit_post_link()wp-includes/link-template.php |
Retrieves the edit post link for post. |
edit_post_link()wp-includes/link-template.php |
Displays the edit post link for post. |
get_delete_post_link()wp-includes/link-template.php |
Retrieves the delete posts link for post. |
get_post_comments_feed_link()wp-includes/link-template.php |
Retrieves the permalink for the post comments feed. |
permalink_anchor()wp-includes/link-template.php |
Displays the permalink anchor for the current post. |
get_permalink()wp-includes/link-template.php |
Retrieves the full permalink for the current post or post ID. |
get_post_permalink()wp-includes/link-template.php |
Retrieves the permalink for a post of a custom post type. |
get_page_link()wp-includes/link-template.php |
Retrieves the permalink for the current page or page ID. |
_get_page_link()wp-includes/link-template.php |
Retrieves the page permalink. |
get_attachment_link()wp-includes/link-template.php |
Retrieves the permalink for an attachment. |
wp_admin_bar_edit_menu()wp-includes/admin-bar.php |
Provides an edit link for posts and terms. |
get_post_thumbnail_id()wp-includes/post-thumbnail-template.php |
Retrieves the post thumbnail ID. |
get_the_post_thumbnail()wp-includes/post-thumbnail-template.php |
Retrieves the post thumbnail. |
Walker_Page::start_el()wp-includes/class-walker-page.php |
Outputs the beginning of the current element in the tree. |
wp_get_attachment_link()wp-includes/post-template.php |
Retrieves an attachment page link using an image or icon, if possible. |
prepend_attachment()wp-includes/post-template.php |
Wraps attachment in paragraph tag before content. |
get_the_password_form()wp-includes/post-template.php |
Retrieves protected post password form content. |
get_page_template_slug()wp-includes/post-template.php |
Gets the specific template filename for a given post. |
wp_post_revision_title()wp-includes/post-template.php |
Retrieves formatted date timestamp of a revision (linked to that revisions’s page). |
wp_post_revision_title_expanded()wp-includes/post-template.php |
Retrieves formatted date timestamp of a revision (linked to that revisions’s page). |
wp_list_post_revisions()wp-includes/post-template.php |
Displays a list of a post’s revisions. |
post_password_required()wp-includes/post-template.php |
Determines whether the post requires password and whether a correct password has been provided. |
_wp_link_page()wp-includes/post-template.php |
Helper function for wp_link_pages() . |
get_the_guid()wp-includes/post-template.php |
Retrieves the Post Global Unique Identifier (guid). |
get_the_content()wp-includes/post-template.php |
Retrieves the post content. |
get_the_excerpt()wp-includes/post-template.php |
Retrieves the post excerpt. |
has_excerpt()wp-includes/post-template.php |
Determines whether the post has a custom excerpt. |
get_post_class()wp-includes/post-template.php |
Retrieves an array of the class names for the post container element. |
get_the_ID()wp-includes/post-template.php |
Retrieves the ID of the current item in the WordPress Loop. |
the_title_attribute()wp-includes/post-template.php |
Sanitizes the current title when retrieving or displaying. |
get_the_title()wp-includes/post-template.php |
Retrieves the post title. |
the_guid()wp-includes/post-template.php |
Displays the Post Global Unique Identifier (guid). |
get_post_galleries()wp-includes/media.php |
Retrieves galleries from the passed post’s content. |
wp_prepare_attachment_for_js()wp-includes/media.php |
Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model. |
wp_enqueue_media()wp-includes/media.php |
Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
get_attached_media()wp-includes/media.php |
Retrieves media attached to the passed post. |
wp_video_shortcode()wp-includes/media.php |
Builds the Video shortcode output. |
get_attachment_taxonomies()wp-includes/media.php |
Retrieves taxonomies attached to given the attachment. |
gallery_shortcode()wp-includes/media.php |
Builds the Gallery shortcode output. |
wp_playlist_shortcode()wp-includes/media.php |
Builds the Playlist shortcode output. |
wp_audio_shortcode()wp-includes/media.php |
Builds the Audio shortcode output. |
wp_get_attachment_image()wp-includes/media.php |
Gets an HTML img element representing an image attachment. |
clean_post_cache()wp-includes/post.php |
Will clean the post in the cache. |
wp_get_post_parent_id()wp-includes/post.php |
Returns the ID of the post’s parent. |
set_post_thumbnail()wp-includes/post.php |
Sets the post thumbnail (featured image) for the given post. |
delete_post_thumbnail()wp-includes/post.php |
Removes the thumbnail (featured image) from the given post. |
wp_mime_type_icon()wp-includes/post.php |
Retrieves the icon for a MIME type or attachment. |
wp_delete_attachment()wp-includes/post.php |
Trashes or deletes an attachment. |
wp_get_attachment_metadata()wp-includes/post.php |
Retrieves attachment metadata for attachment ID. |
wp_update_attachment_metadata()wp-includes/post.php |
Updates metadata for an attachment. |
wp_get_attachment_url()wp-includes/post.php |
Retrieves the URL for an attachment. |
wp_get_attachment_thumb_file()wp-includes/deprecated.php |
Retrieves thumbnail for an attachment. |
get_pung()wp-includes/post.php |
Retrieves URLs already pinged for a post. |
get_to_ping()wp-includes/post.php |
Retrieves URLs that need to be pinged. |
trackback_url_list()wp-includes/post.php |
Does trackbacks for a list of URLs. |
get_page()wp-includes/post.php |
Retrieves page data given a page ID or page object. |
get_page_by_path()wp-includes/post.php |
Retrieves a page given its path. |
get_page_uri()wp-includes/post.php |
Builds the URI path for a page. |
is_local_attachment()wp-includes/post.php |
Determines whether an attachment URI is local and really an attachment. |
get_page_by_title()wp-includes/deprecated.php |
Retrieves a page given its title. |
wp_publish_post()wp-includes/post.php |
Publishes a post by transitioning the post status. |
check_and_publish_future_post()wp-includes/post.php |
Publishes future post and make sure post ID has future post status. |
wp_unique_post_slug()wp-includes/post.php |
Computes a unique slug for the post, when given the desired slug and some post details. |
add_ping()wp-includes/post.php |
Adds a URL to those already pinged. |
wp_untrash_post_comments()wp-includes/post.php |
Restores comments for a post from the Trash. |
wp_insert_post()wp-includes/post.php |
Inserts or update a post. |
wp_update_post()wp-includes/post.php |
Updates a post with new post data. |
wp_delete_post()wp-includes/post.php |
Trashes or deletes a post or page. |
_reset_front_page_settings_for_post()wp-includes/post.php |
Resets the page_on_front, show_on_front, and page_for_post settings when a linked page is deleted or trashed. |
wp_trash_post()wp-includes/post.php |
Moves a post or page to the Trash |
wp_untrash_post()wp-includes/post.php |
Restores a post from the Trash. |
wp_trash_post_comments()wp-includes/post.php |
Moves comments for a post to the Trash. |
get_post_ancestors()wp-includes/post.php |
Retrieves the IDs of the ancestors of a post. |
get_post_field()wp-includes/post.php |
Retrieves data from a post field based on Post ID. |
get_post_mime_type()wp-includes/post.php |
Retrieves the mime type of an attachment based on the ID. |
get_post_status()wp-includes/post.php |
Retrieves the post status based on the post ID. |
get_post_type()wp-includes/post.php |
Retrieves the post type of the current post or of a given post. |
update_attached_file()wp-includes/post.php |
Updates attachment file path based on attachment ID. |
redirect_canonical()wp-includes/canonical.php |
Redirects incoming links to the proper URL based on the site url. |
url_to_postid()wp-includes/rewrite.php |
Examines a URL and try to determine the post ID it represents. |
_wp_put_post_revision()wp-includes/revision.php |
Inserts post data into the posts table as a post revision. |
wp_get_post_revision()wp-includes/revision.php |
Gets a post revision. |
wp_get_post_revisions()wp-includes/revision.php |
Returns all revisions of specified post. |
_wp_preview_terms_filter()wp-includes/revision.php |
Filters terms lookup to set the post format. |
_wp_post_revision_fields()wp-includes/revision.php |
Determines which fields of posts are to be saved in revisions. |
wp_save_post_revision()wp-includes/revision.php |
Creates a revision for the current version of a post. |
wp_get_post_autosave()wp-includes/revision.php |
Retrieves the autosaved data of the specified post. |
get_blog_post()wp-includes/ms-functions.php |
Gets a blog post from any site on the network. |
get_post_format()wp-includes/post-formats.php |
Retrieve the format slug for a post |
set_post_format()wp-includes/post-formats.php |
Assign a format to a post |
get_the_author_posts()wp-includes/author-template.php |
Retrieves the number of posts by the author of the current post. |
_update_blog_date_on_post_delete()wp-includes/ms-blogs.php |
Handler for updating the current site’s last updated date when a published post is deleted. |
get_the_modified_author()wp-includes/author-template.php |
Retrieves the author who last edited the current post. |
wp_setup_nav_menu_item()wp-includes/nav-menu.php |
Decorates a menu item object with the shared navigation menu item properties. |
wp_update_nav_menu_item()wp-includes/nav-menu.php |
Saves the properties of a menu item or create a new one. |
wp_xmlrpc_server::mt_getPostCategories()wp-includes/class-wp-xmlrpc-server.php |
Retrieves post categories. |
wp_xmlrpc_server::mt_setPostCategories()wp-includes/class-wp-xmlrpc-server.php |
Sets categories for a post. |
wp_xmlrpc_server::mt_getTrackbackPings()wp-includes/class-wp-xmlrpc-server.php |
Retrieves trackbacks sent to a given post. |
wp_xmlrpc_server::mt_publishPost()wp-includes/class-wp-xmlrpc-server.php |
Sets a post’s publish status to ‘publish’. |
wp_xmlrpc_server::pingback_ping()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a pingback and registers it. |
wp_xmlrpc_server::pingback_extensions_getPingbacks()wp-includes/class-wp-xmlrpc-server.php |
Retrieves an array of URLs that pingbacked the given URL. |
wp_xmlrpc_server::mw_editPost()wp-includes/class-wp-xmlrpc-server.php |
Edits a post. |
wp_xmlrpc_server::mw_getPost()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a post. |
wp_xmlrpc_server::mw_newMediaObject()wp-includes/class-wp-xmlrpc-server.php |
Uploads a file, following your settings. |
wp_xmlrpc_server::blogger_getPost()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a post. |
wp_xmlrpc_server::blogger_editPost()wp-includes/class-wp-xmlrpc-server.php |
Edits a post. |
wp_xmlrpc_server::blogger_deletePost()wp-includes/class-wp-xmlrpc-server.php |
Deletes a post. |
wp_xmlrpc_server::wp_getMediaItem()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a media item by ID. |
wp_xmlrpc_server::wp_getRevisions()wp-includes/class-wp-xmlrpc-server.php |
Retrieves revisions for a specific post. |
wp_xmlrpc_server::wp_restoreRevision()wp-includes/class-wp-xmlrpc-server.php |
Restores a post revision. |
wp_xmlrpc_server::wp_newComment()wp-includes/class-wp-xmlrpc-server.php |
Creates a new comment. |
wp_xmlrpc_server::wp_getCommentCount()wp-includes/class-wp-xmlrpc-server.php |
Retrieves comment counts. |
wp_xmlrpc_server::wp_deletePage()wp-includes/class-wp-xmlrpc-server.php |
Deletes a page. |
wp_xmlrpc_server::wp_editPage()wp-includes/class-wp-xmlrpc-server.php |
Edits a page. |
wp_xmlrpc_server::wp_getPage()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a page. |
wp_xmlrpc_server::_prepare_page()wp-includes/class-wp-xmlrpc-server.php |
Prepares page data for return in an XML-RPC object. |
wp_xmlrpc_server::_insert_post()wp-includes/class-wp-xmlrpc-server.php |
Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
wp_xmlrpc_server::wp_editPost()wp-includes/class-wp-xmlrpc-server.php |
Edits a post for any registered post type. |
wp_xmlrpc_server::wp_deletePost()wp-includes/class-wp-xmlrpc-server.php |
Deletes a post for any registered post type. |
wp_xmlrpc_server::wp_getPost()wp-includes/class-wp-xmlrpc-server.php |
Retrieves a post. |
wp_xmlrpc_server::_prepare_post()wp-includes/class-wp-xmlrpc-server.php |
Prepares post data for return in an XML-RPC object. |
WP_Customize_Control::render_content()wp-includes/class-wp-customize-control.php |
Renders the control’s content. |
get_post_reply_link()wp-includes/comment-template.php |
Retrieves HTML content for reply to post link. |
get_cancel_comment_reply_link()wp-includes/comment-template.php |
Retrieves HTML content for cancel comment reply link. |
get_comment_id_fields()wp-includes/comment-template.php |
Retrieves hidden input HTML for replying to comments. |
comment_form_title()wp-includes/comment-template.php |
Displays text based on comment reply status. |
comment_form()wp-includes/comment-template.php |
Outputs a complete commenting form for use within a template. |
comments_open()wp-includes/comment-template.php |
Determines whether the current post is open for comments. |
pings_open()wp-includes/comment-template.php |
Determines whether the current post is open for pings. |
wp_comment_form_unfiltered_html_nonce()wp-includes/comment-template.php |
Displays form token for unfiltered comments. |
get_comment_reply_link()wp-includes/comment-template.php |
Retrieves HTML content for reply to comment link. |
get_comments_number()wp-includes/comment-template.php |
Retrieves the amount of comments a post has. |
get_comment_class()wp-includes/comment-template.php |
Returns the classes for the comment div as an array. |
_close_comments_for_old_post()wp-includes/comment.php |
Closes comments on an old post. Hooked to comments_open and pings_open. |
wp_update_comment()wp-includes/comment.php |
Updates an existing comment in the database. |
wp_update_comment_count_now()wp-includes/comment.php |
Updates the comment count for the post. |
do_trackbacks()wp-includes/comment.php |
Performs trackbacks. |
pingback()wp-includes/comment.php |
Pings back the links found in a post. |
_WP_Editors::editor_settings()wp-includes/class-wp-editor.php |
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
Skip to note 9 content
Fiaz Husyn
For reference, WP_Post OBJECT contains following fields:
Skip to note 10 content
beachaney
Wouldn’t it be better practice to use get_the_title(..) in this case? directly accessing the post object’s data member would bypass applying filters and enforcing protected and private settings, unless that’s explicitly desired.
Skip to note 11 content
rjksn
If you need special things—[shortcodes], paragraph tags, anything exciting—in the content, you should apply the filters as opposed to using do_shortcode() .
$post = get_post( 42 ); $output = apply_filters( 'the_content', $post->post_content );Skip to note 12 content
Codex
To get the title for a post with ID 7:
$post_7 = get_post( 7 ); $title = $post_7->post_title;Alternatively, specify the $output parameter:
$post_7 = get_post( 7, ARRAY_A ); $title = $post_7['post_title'];Skip to note 13 content
ancawonka
If you want to get a post by slug, you could do a new WP_Query, or use the get_page_by_path function: https://developer.wordpress.org/reference/functions/get_page_by_path/
You’ll need to use the post_type if you are not getting a page.
Skip to note 14 content
Fiaz Husyn
For Reference : WP_Post Object has following properties, which are returned by get_post() .
Member Variable Variable Type Notes ID int The ID of the post post_author string The post author's user ID (numeric string) post_name string The post's slug post_type string See Post Types post_title string The title of the post post_date string Format: 0000-00-00 00:00:00 post_date_gmt string Format: 0000-00-00 00:00:00 post_content string The full content of the post post_excerpt string User-defined post excerpt post_status string See get_post_status for values comment_status string Returns: { open, closed } ping_status string Returns: { open, closed } post_password string Returns empty string if no password post_parent int Parent Post ID (default 0) post_modified string Format: 0000-00-00 00:00:00 post_modified_gmt string Format: 0000-00-00 00:00:00 comment_count string Number of comments on post (numeric string) menu_order string Order value as set through page-attribute when enabled (numeric string. Defaults to 0)Skip to note 15 content
Riyaz
To Get author of the Post
$post_info = get_post( 10 ); $author = $post_info->post_author;Skip to note 16 content
Al
If you have a shortcode in the content you should use:
$post = get_post( 4304,ARRAY_A ); $output = do_shortcode($post['post_content']);post_content:$page = get_post( 1 ); echo __( wp_kses_post( $page->post_content ) );Change
1to the post ID.