返回介绍

get_post()

发布于 2017-09-10 23:48:18 字数 29949 浏览 1094 评论 0 收藏 0

get_post( int|WP_Post|null $post = null,  string $output = OBJECT,  string $filter = 'raw' )

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.


参数

$post

(int|WP_Post|null) (Optional) Post ID or post object. Defaults to global $post.

Default value: null

$output

(string) (Optional) 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 value: OBJECT

$filter

(string) (Optional) Type of filter to apply. Accepts 'raw', 'edit', 'db', or 'display'.

Default value: 'raw'


返回值

(WP_Post|array|null) Type corresponding to $output on success or null on failure. When $output is OBJECT, a WP_Post instance is returned.


源代码

File: wp-includes/post.php

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 ( $output == ARRAY_A )
		return $_post->to_array();
	elseif ( $output == ARRAY_N )
		return array_values( $_post->to_array() );

	return $_post;
}

更新日志

Versiondescription
1.5.1Introduced.

相关函数

Uses

  • wp-includes/class-wp-post.php: WP_Post::__construct()
  • wp-includes/class-wp-post.php: WP_Post::get_instance()
  • wp-includes/post.php: sanitize_post()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::get_revision()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::get_parent()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_comment()
  • wp-includes/nav-menu.php: _wp_delete_customize_changeset_dependent_auto_drafts()
  • wp-includes/widgets/class-wp-widget-media-audio.php: WP_Widget_Media_Audio::render_media()
  • wp-includes/widgets/class-wp-widget-media-video.php: WP_Widget_Media_Video::render_media()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::display_media_state()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::is_attachment_with_mime_type()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::_publish_changeset_values()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save_changeset_post()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::get_changeset_post_data()
  • wp-includes/theme.php: wp_update_custom_css_post()
  • wp-includes/theme.php: wp_get_custom_css_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::handle_template()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_database()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::delete_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::prepare_links()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_items_permissions_check()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::insert_auto_draft_post()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::get_original_title()
  • wp-includes/revision.php: _wp_preview_post_thumbnail_filter()
  • wp-includes/link-template.php: wp_get_canonical_url()
  • wp-includes/post.php: wp_get_attachment_caption()
  • wp-includes/revision.php: _wp_post_revision_data()
  • wp-includes/post.php: wp_add_trashed_suffix_to_post_name_for_post()
  • wp-includes/embed.php: get_post_embed_url()
  • wp-includes/embed.php: get_post_embed_html()
  • wp-includes/embed.php: get_oembed_response_data()
  • wp-includes/class-wp-comment.php: WP_Comment::__isset()
  • wp-includes/class-wp-comment.php: WP_Comment::__get()
  • wp-includes/comment.php: wp_handle_comment_submission()
  • wp-includes/link-template.php: get_preview_post_link()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::value()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::load_available_items_query()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_title()
  • wp-admin/includes/class-wp-site-icon.php: WP_Site_Icon::create_attachment_object()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_parent()
  • wp-includes/post.php: wp_attachment_is()
  • wp-includes/class-wp-query.php: WP_Query::setup_postdata()
  • wp-includes/ms-blogs.php: _update_posts_count_on_delete()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_media_shortcode()
  • wp-admin/includes/class-file-upload-upgrader.php: File_Upload_Upgrader::__construct()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::get()
  • wp-admin/includes/export.php: wxr_post_taxonomy()
  • wp-admin/includes/deprecated.php: get_post_to_edit()
  • wp-admin/includes/image-edit.php: stream_preview_image()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/dashboard.php: wp_dashboard_quick_press()
  • wp-admin/includes/template.php: the_post_password()
  • wp-admin/includes/template.php: meta_form()
  • wp-admin/includes/template.php: touch_time()
  • wp-admin/includes/template.php: parent_dropdown()
  • wp-admin/includes/template.php: wp_popular_terms_checklist()
  • wp-admin/includes/media.php: media_upload_flash_bypass()
  • wp-admin/includes/media.php: attachment_submitbox_metadata()
  • wp-admin/includes/media.php: image_media_send_to_editor()
  • wp-admin/includes/media.php: get_attachment_fields_to_edit()
  • wp-admin/includes/media.php: get_media_items()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/includes/media.php: get_compat_media_markup()
  • wp-admin/includes/media.php: media_upload_form_handler()
  • wp-admin/includes/media.php: media_handle_upload()
  • wp-admin/includes/media.php: media_handle_sideload()
  • wp-admin/includes/media.php: media_buttons()
  • wp-admin/includes/post.php: get_sample_permalink_html()
  • wp-admin/includes/post.php: _wp_post_thumbnail_html()
  • wp-admin/includes/post.php: wp_check_post_lock()
  • wp-admin/includes/post.php: wp_set_post_lock()
  • wp-admin/includes/post.php: _admin_notice_post_locked()
  • wp-admin/includes/post.php: wp_create_post_autosave()
  • wp-admin/includes/post.php: post_preview()
  • wp-admin/includes/post.php: wp_autosave()
  • wp-admin/includes/post.php: _fix_attachment_links()
  • wp-admin/includes/post.php: get_sample_permalink()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_order()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_attachment_to_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_link_to_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_revision_diffs()
  • wp-admin/includes/post.php: edit_post()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/post.php: get_default_post_to_edit()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_fullscreen_save_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_remove_post_lock()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_compat()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_menu_item()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_meta()
  • wp-admin/includes/ajax-actions.php: wp_ajax_inline_save()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_trash_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_page()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-admin/includes/revision.php: wp_get_revision_ui_diff()
  • wp-admin/includes/revision.php: wp_prepare_revisions_for_js()
  • wp-admin/includes/class-wp-comments-list-table.php: 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-walker-nav-menu-edit.php: Walker_Nav_Menu_Edit::start_el()
  • wp-admin/includes/nav-menu.php: _wp_ajax_menu_quick_search()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_item_post_type_meta_box()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::single_row()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::_page_rows()
  • wp-admin/custom-header.php: Custom_Image_Header::create_attachment_object()
  • wp-includes/capabilities.php: author_can()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/category-template.php: has_term()
  • wp-includes/category-template.php: get_the_terms()
  • wp-includes/pluggable.php: wp_notify_postauthor()
  • wp-includes/pluggable.php: wp_notify_moderator()
  • wp-includes/general-template.php: feed_links_extra()
  • wp-includes/general-template.php: get_the_modified_date()
  • wp-includes/general-template.php: get_the_time()
  • wp-includes/general-template.php: get_post_time()
  • wp-includes/general-template.php: get_the_modified_time()
  • wp-includes/general-template.php: get_post_modified_time()
  • wp-includes/general-template.php: the_weekday()
  • wp-includes/general-template.php: the_weekday_date()
  • wp-includes/general-template.php: the_date_xml()
  • wp-includes/general-template.php: get_the_date()
  • wp-includes/deprecated.php: wp_get_single_post()
  • wp-includes/deprecated.php: get_parent_post_rel_link()
  • wp-includes/deprecated.php: get_the_attachment_link()
  • wp-includes/deprecated.php: get_attachment_icon_src()
  • wp-includes/deprecated.php: get_attachment_icon()
  • wp-includes/deprecated.php: get_attachment_innerHTML()
  • wp-includes/deprecated.php: user_can_edit_post()
  • wp-includes/deprecated.php: get_postdata()
  • wp-includes/deprecated.php: start_wp()
  • wp-includes/class-wp-query.php: WP_Query::get_queried_object()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/functions.php: wp_scheduled_delete()
  • wp-includes/class-wp-embed.php: WP_Embed::cache_oembed()
  • wp-includes/class-wp-embed.php: WP_Embed::maybe_run_ajax_cache()
  • wp-includes/class-wp-embed.php: WP_Embed::shortcode()
  • wp-includes/taxonomy.php: get_the_taxonomies()
  • wp-includes/taxonomy.php: get_post_taxonomies()
  • wp-includes/link-template.php: wp_get_shortlink()
  • wp-includes/link-template.php: the_shortlink()
  • wp-includes/link-template.php: get_adjacent_post_link()
  • wp-includes/link-template.php: get_adjacent_post()
  • wp-includes/link-template.php: get_adjacent_post_rel_link()
  • wp-includes/link-template.php: get_boundary_post()
  • wp-includes/link-template.php: get_edit_post_link()
  • wp-includes/link-template.php: edit_post_link()
  • wp-includes/link-template.php: get_delete_post_link()
  • wp-includes/link-template.php: get_post_comments_feed_link()
  • wp-includes/link-template.php: permalink_anchor()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: get_post_permalink()
  • wp-includes/link-template.php: get_page_link()
  • wp-includes/link-template.php: _get_page_link()
  • wp-includes/link-template.php: get_attachment_link()
  • wp-includes/admin-bar.php: wp_admin_bar_edit_menu()
  • wp-includes/post-thumbnail-template.php: get_post_thumbnail_id()
  • wp-includes/post-thumbnail-template.php: get_the_post_thumbnail()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/post-template.php: prepend_attachment()
  • wp-includes/post-template.php: get_the_password_form()
  • wp-includes/post-template.php: get_page_template_slug()
  • wp-includes/post-template.php: wp_post_revision_title()
  • wp-includes/post-template.php: wp_post_revision_title_expanded()
  • wp-includes/post-template.php: wp_list_post_revisions()
  • wp-includes/class-walker-page.php: Walker_Page::start_el()
  • wp-includes/post-template.php: post_password_required()
  • wp-includes/post-template.php: _wp_link_page()
  • wp-includes/post-template.php: get_the_guid()
  • wp-includes/post-template.php: get_the_content()
  • wp-includes/post-template.php: get_the_excerpt()
  • wp-includes/post-template.php: has_excerpt()
  • wp-includes/post-template.php: get_post_class()
  • wp-includes/post-template.php: get_body_class()
  • wp-includes/media.php: get_post_galleries()
  • wp-includes/post-template.php: get_the_ID()
  • wp-includes/post-template.php: the_title_attribute()
  • wp-includes/post-template.php: get_the_title()
  • wp-includes/post-template.php: the_guid()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: wp_enqueue_media()
  • wp-includes/media.php: get_attached_media()
  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/media.php: adjacent_image_link()
  • wp-includes/media.php: get_attachment_taxonomies()
  • wp-includes/media.php: gallery_shortcode()
  • wp-includes/media.php: wp_playlist_shortcode()
  • wp-includes/media.php: wp_audio_shortcode()
  • wp-includes/media.php: wp_get_attachment_image()
  • wp-includes/post.php: clean_post_cache()
  • wp-includes/post.php: wp_get_post_parent_id()
  • wp-includes/post.php: set_post_thumbnail()
  • wp-includes/post.php: delete_post_thumbnail()
  • wp-includes/post.php: wp_mime_type_icon()
  • wp-includes/post.php: wp_get_attachment_metadata()
  • wp-includes/post.php: wp_update_attachment_metadata()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: wp_get_attachment_thumb_file()
  • wp-includes/post.php: wp_get_attachment_thumb_url()
  • wp-includes/post.php: get_pung()
  • wp-includes/post.php: get_to_ping()
  • wp-includes/post.php: trackback_url_list()
  • wp-includes/post.php: get_page()
  • wp-includes/post.php: get_page_by_path()
  • wp-includes/post.php: get_page_by_title()
  • wp-includes/post.php: get_page_uri()
  • wp-includes/post.php: is_local_attachment()
  • wp-includes/post.php: wp_publish_post()
  • wp-includes/post.php: check_and_publish_future_post()
  • wp-includes/post.php: wp_unique_post_slug()
  • wp-includes/post.php: add_ping()
  • wp-includes/post.php: wp_untrash_post_comments()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/post.php: _reset_front_page_settings_for_post()
  • wp-includes/post.php: wp_trash_post()
  • wp-includes/post.php: wp_untrash_post()
  • wp-includes/post.php: wp_trash_post_comments()
  • wp-includes/post.php: get_post_ancestors()
  • wp-includes/post.php: get_post_field()
  • wp-includes/post.php: get_post_mime_type()
  • wp-includes/post.php: get_post_status()
  • wp-includes/post.php: get_post_type()
  • wp-includes/post.php: update_attached_file()
  • wp-includes/rewrite.php: url_to_postid()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/revision.php: _wp_put_post_revision()
  • wp-includes/revision.php: wp_get_post_revision()
  • wp-includes/revision.php: wp_get_post_revisions()
  • wp-includes/revision.php: _wp_preview_terms_filter()
  • wp-includes/revision.php: _wp_post_revision_fields()
  • wp-includes/revision.php: wp_save_post_revision()
  • wp-includes/ms-functions.php: get_blog_post()
  • wp-includes/post-formats.php: get_post_format()
  • wp-includes/post-formats.php: set_post_format()
  • wp-includes/author-template.php: get_the_author_posts()
  • wp-includes/ms-blogs.php: _update_blog_date_on_post_delete()
  • wp-includes/author-template.php: get_the_modified_author()
  • wp-includes/nav-menu.php: wp_setup_nav_menu_item()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_getPostCategories()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_setPostCategories()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_getTrackbackPings()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_publishPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::pingback_ping()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::pingback_extensions_getPingbacks()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_editPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_getPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_newMediaObject()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_getPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_editPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_deletePost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getMediaItem()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getRevisions()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_restoreRevision()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_newComment()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getCommentCount()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deletePage()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_editPage()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPage()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_page()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_editPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deletePost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_post()
  • wp-includes/class-wp-customize-control.php: WP_Customize_Control::render_content()
  • wp-includes/comment-template.php: get_post_reply_link()
  • wp-includes/comment-template.php: comments_open()
  • wp-includes/comment-template.php: pings_open()
  • wp-includes/comment-template.php: wp_comment_form_unfiltered_html_nonce()
  • wp-includes/comment-template.php: get_comment_reply_link()
  • wp-includes/comment-template.php: get_comments_number()
  • wp-includes/comment-template.php: get_comment_class()
  • wp-includes/comment.php: _close_comments_for_old_post()
  • wp-includes/comment.php: wp_update_comment()
  • wp-includes/comment.php: wp_update_comment_count_now()
  • wp-includes/comment.php: do_trackbacks()
  • wp-includes/comment.php: pingback()
  • wp-includes/class-wp-editor.php: _WP_Editors::editor_settings()
  • Show 288 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 21You must log in to vote on the helpfulness of this note Contributed by Fiaz Husyn

    For reference, WP_Post OBJECT contains following fields:

    
    WP_Post Object
    (
        [ID] =>
        [post_author] =>
        [post_date] => 
        [post_date_gmt] => 
        [post_content] => 
        [post_title] => 
        [post_excerpt] => 
        [post_status] =>
        [comment_status] =>
        [ping_status] => 
        [post_password] => 
        [post_name] =>
        [to_ping] => 
        [pinged] => 
        [post_modified] => 
        [post_modified_gmt] =>
        [post_content_filtered] => 
        [post_parent] => 
        [guid] => 
        [menu_order] =>
        [post_type] =>
        [post_mime_type] => 
        [comment_count] =>
        [filter] =>
    )
    
  2. 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'];
    

    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.

    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 );
    

    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) 
    

    If you have a shortcode in the content you should use:

    $post = get_post( 4304,ARRAY_A );
    $output =  do_shortcode($post['post_content']);

    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.

    To Get author of the Post

    
    $post_info = get_post( 10 );
    $author = $post_info->post_author;
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文