- How to Edit Post Content Programmatically in WordPress
- About Varadharaj V
- Related Articles
- Creating Custom Options Page in WordPress
- Sortable Data Table WordPress Front&Back-end
- Custom Post Type and Taxonomy Permalink Creation
- How to Automatically Change a Post’s Status in WordPress
- 2 comments
- edit_post() │ WP 1.5.0
- Возвращает
- Использование
- Заметки
- Список изменений
- Код edit_post() edit post WP 6.2.2
- Using WordPress ‘edit_post()’ PHP function
- Usage
- Parameters
- More information
- Examples
- Basic usage
- Update post status
- Add a post excerpt
- Change post author
- Update post with $_POST superglobal
- edit_post() │ WP 1.5.0
- Return
- Usage
- Notes
- Changelog
- edit_post() edit post code WP 6.2.2
How to Edit Post Content Programmatically in WordPress
In this Article I am going to edit post content dynamically, with that existing content, I am going to add a footer text like, ” With regards, Author_name,” This not necessary here, but I am just taking it as example. First we need to query the posts using query_post and filter with certain parameters, than choose our necessary post, edit it.
get_row("SELECT post_content,post_title FROM $wpdb->posts WHERE the post title and content $kv_post_title = $kv_post->post_content; $kv_post_content = $kv_post->post_title; ?>
Now, you can print or edit the post title and content using the above two variables. $kv_post_title and $kv_post_content . Here you can simply pass the $kv_post_content and title to wp_editor and create editable fields. Else you can use php function to editing the content and upload it again by using wp_update_post(). Like the following example.
$kv_edited_post = array( 'ID' => 10, 'post_title' => $kv_post_title, 'post_content' => $kv_post_content ); wp_update_post( $kv_edited_post);
That’s it, Now you can edit your posts through programmatically. If you really like my article, Just follow me on below social sites and get more updates from me. If you have any doubt or require further clarification just comment it on next tab. Let me help you to fix it.
About Varadharaj V
The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level
Related Articles
Creating Custom Options Page in WordPress
Sortable Data Table WordPress Front&Back-end
Custom Post Type and Taxonomy Permalink Creation
How to Automatically Change a Post’s Status in WordPress
2 comments
I want to do exactly this sort of thing – update broken links by going through all my (thousands of) posts doing a search/replace of certain types of links known to be broken. However… where does one put code like that you show above? I’m a little bit familiar with WP in that I’ve written PHP functions to add some dynamic content to my blog, but what I’m looking for is not something that generates blog content, but instead makes a once-through pass through all of my blog posts and updates them. Where does one put this sort of code?
Thanks!
Hi wil,
this is a sample snippet to edit a post through programmatically, and there you see I queried with a post So you need to fetch the post Id’s in a loop and query each one by iterating the loop, than you can get each post if you want to append or replace anything , you can do by using normal PHP functions of string .
edit_post() │ WP 1.5.0
Updates an existing post with values provided in $_POST . If post data is passed as an argument, it is treated as an array of data keyed appropriately for turning into a post object. If post data is not passed, the $_POST global variable is used instead.
Хуки из функции
Возвращает
Использование
Заметки
Список изменений
Код edit_post() edit post WP 6.2.2
function edit_post( $post_data = null ) < global $wpdb; if ( empty( $post_data ) ) < $post_data = &$_POST; >// Clear out any data in internal vars. unset( $post_data['filter'] ); $post_id = (int) $post_data['post_ID']; $post = get_post( $post_id ); $post_data['post_type'] = $post->post_type; $post_data['post_mime_type'] = $post->post_mime_type; if ( ! empty( $post_data['post_status'] ) ) < $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); if ( 'inherit' === $post_data['post_status'] ) < unset( $post_data['post_status'] ); >> $ptype = get_post_type_object( $post_data['post_type'] ); if ( ! current_user_can( 'edit_post', $post_id ) ) < if ( 'page' === $post_data['post_type'] ) < wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); >else < wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); >> if ( post_type_supports( $ptype->name, 'revisions' ) ) < $revisions = wp_get_post_revisions( $post_id, array( 'order' =>'ASC', 'posts_per_page' => 1, ) ); $revision = current( $revisions ); // Check if the revisions have been upgraded. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) < _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_id ) ); >> if ( isset( $post_data['visibility'] ) ) < switch ( $post_data['visibility'] ) < case 'public': $post_data['post_password'] = ''; break; case 'password': unset( $post_data['sticky'] ); break; case 'private': $post_data['post_status'] = 'private'; $post_data['post_password'] = ''; unset( $post_data['sticky'] ); break; >> $post_data = _wp_translate_postdata( true, $post_data ); if ( is_wp_error( $post_data ) ) < wp_die( $post_data->get_error_message() ); > $translated = _wp_get_allowed_postdata( $post_data ); // Post formats. if ( isset( $post_data['post_format'] ) ) < set_post_format( $post_id, $post_data['post_format'] ); >$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' ); foreach ( $format_meta_urls as $format_meta_url ) < $keyed = '_format_' . $format_meta_url; if ( isset( $post_data[ $keyed ] ) ) < update_post_meta( $post_id, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) ); >> $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' ); foreach ( $format_keys as $key ) < $keyed = '_format_' . $key; if ( isset( $post_data[ $keyed ] ) ) < if ( current_user_can( 'unfiltered_html' ) ) < update_post_meta( $post_id, $keyed, $post_data[ $keyed ] ); >else < update_post_meta( $post_id, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) ); >> > if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) < $id3data = wp_get_attachment_metadata( $post_id ); if ( ! is_array( $id3data ) ) < $id3data = array(); >foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) < if ( isset( $post_data[ 'id3_' . $key ] ) ) < $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) ); >> wp_update_attachment_metadata( $post_id, $id3data ); > // Meta stuff. if ( isset( $post_data['meta'] ) && $post_data['meta'] ) < foreach ( $post_data['meta'] as $key =>$value ) < $meta = get_post_meta_by_id( $key ); if ( ! $meta ) < continue; >if ( $meta->post_id != $post_id ) < continue; >if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_id, $meta->meta_key ) ) < continue; >if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_id, $value['key'] ) ) < continue; >update_meta( $key, $value['key'], $value['value'] ); > > if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) < foreach ( $post_data['deletemeta'] as $key =>$value ) < $meta = get_post_meta_by_id( $key ); if ( ! $meta ) < continue; >if ( $meta->post_id != $post_id ) < continue; >if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_id, $meta->meta_key ) ) < continue; >delete_meta( $key ); > > // Attachment stuff. if ( 'attachment' === $post_data['post_type'] ) < if ( isset( $post_data['_wp_attachment_image_alt'] ) ) < $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); if ( get_post_meta( $post_id, '_wp_attachment_image_alt', true ) !== $image_alt ) < $image_alt = wp_strip_all_tags( $image_alt, true ); // update_post_meta() expects slashed. update_post_meta( $post_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); >> $attachment_data = isset( $post_data['attachments'][ $post_id ] ) ? $post_data['attachments'][ $post_id ] : array(); /** This filter is documented in wp-admin/includes/media.php */ $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data ); > // Convert taxonomy input to term IDs, to avoid ambiguity. if ( isset( $post_data['tax_input'] ) ) < foreach ( (array) $post_data['tax_input'] as $taxonomy =>$terms ) < $tax_object = get_taxonomy( $taxonomy ); if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) < $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) ); > > > add_meta( $post_id ); update_post_meta( $post_id, '_edit_last', get_current_user_id() ); $success = wp_update_post( $translated ); // If the save failed, see if we can sanity check the main fields and try again. if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) < $fields = array( 'post_title', 'post_content', 'post_excerpt' ); foreach ( $fields as $field ) < if ( isset( $translated[ $field ] ) ) < $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] ); > > wp_update_post( $translated ); > // Now that we have an ID we can fix any attachment anchor hrefs. _fix_attachment_links( $post_id ); wp_set_post_lock( $post_id ); if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) < if ( ! empty( $post_data['sticky'] ) ) < stick_post( $post_id ); >else < unstick_post( $post_id ); >> return $post_id; >
Using WordPress ‘edit_post()’ PHP function
The edit_post() WordPress PHP function is used to update an existing post with values that are provided in the $_POST superglobal. If post data is given as an argument, it’s processed as an array of data, appropriately keyed for conversion into a post object.
Usage
Here’s a basic usage of the edit_post() function:
$post_data = array( 'ID' => 1, 'post_title' => 'Updated Title', 'post_content' => 'This is the updated content.' ); edit_post( $post_data );
In this case, the function is used to update the post with ID 1. The title is updated to ‘Updated Title’ and the content is updated to ‘This is the updated content.’
Parameters
- $post_data (array|null) (Optional) – This is the array of post data to process. If not specified, the function defaults to the $_POST superglobal. The default value is null.
More information
See WordPress Developer Resources: edit_post()
Examples
Basic usage
$post_data = array( 'ID' => 10, 'post_title' => 'My New Title', 'post_content' => 'New content for the post.' ); edit_post( $post_data ); // Updates the post with ID 10
The above code changes the title and content of the post with ID 10.
Update post status
$post_data = array( 'ID' => 5, 'post_status' => 'draft' ); edit_post( $post_data ); // Changes the post status to draft
This example changes the status of the post with ID 5 to ‘draft’.
Add a post excerpt
$post_data = array( 'ID' => 3, 'post_excerpt' => 'This is a short summary of the post.' ); edit_post( $post_data ); // Adds an excerpt to the post
In this case, an excerpt is added to the post with ID 3.
Change post author
$post_data = array( 'ID' => 7, 'post_author' => 2 ); edit_post( $post_data ); // Changes the post author
This example changes the author of the post with ID 7 to the user with ID 2.
Update post with $_POST superglobal
Assuming that $_POST contains relevant data:
edit_post(); // Updates the post using $_POST data
This example updates a post using data from the $_POST superglobal.
edit_post() │ WP 1.5.0
Updates an existing post with values provided in $_POST . If post data is passed as an argument, it is treated as an array of data keyed appropriately for turning into a post object. If post data is not passed, the $_POST global variable is used instead.
Hooks from the function
Return
Usage
Notes
Changelog
edit_post() edit post code WP 6.2.2
function edit_post( $post_data = null ) < global $wpdb; if ( empty( $post_data ) ) < $post_data = &$_POST; >// Clear out any data in internal vars. unset( $post_data['filter'] ); $post_id = (int) $post_data['post_ID']; $post = get_post( $post_id ); $post_data['post_type'] = $post->post_type; $post_data['post_mime_type'] = $post->post_mime_type; if ( ! empty( $post_data['post_status'] ) ) < $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); if ( 'inherit' === $post_data['post_status'] ) < unset( $post_data['post_status'] ); >> $ptype = get_post_type_object( $post_data['post_type'] ); if ( ! current_user_can( 'edit_post', $post_id ) ) < if ( 'page' === $post_data['post_type'] ) < wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); >else < wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); >> if ( post_type_supports( $ptype->name, 'revisions' ) ) < $revisions = wp_get_post_revisions( $post_id, array( 'order' =>'ASC', 'posts_per_page' => 1, ) ); $revision = current( $revisions ); // Check if the revisions have been upgraded. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) < _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_id ) ); >> if ( isset( $post_data['visibility'] ) ) < switch ( $post_data['visibility'] ) < case 'public': $post_data['post_password'] = ''; break; case 'password': unset( $post_data['sticky'] ); break; case 'private': $post_data['post_status'] = 'private'; $post_data['post_password'] = ''; unset( $post_data['sticky'] ); break; >> $post_data = _wp_translate_postdata( true, $post_data ); if ( is_wp_error( $post_data ) ) < wp_die( $post_data->get_error_message() ); > $translated = _wp_get_allowed_postdata( $post_data ); // Post formats. if ( isset( $post_data['post_format'] ) ) < set_post_format( $post_id, $post_data['post_format'] ); >$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' ); foreach ( $format_meta_urls as $format_meta_url ) < $keyed = '_format_' . $format_meta_url; if ( isset( $post_data[ $keyed ] ) ) < update_post_meta( $post_id, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) ); >> $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' ); foreach ( $format_keys as $key ) < $keyed = '_format_' . $key; if ( isset( $post_data[ $keyed ] ) ) < if ( current_user_can( 'unfiltered_html' ) ) < update_post_meta( $post_id, $keyed, $post_data[ $keyed ] ); >else < update_post_meta( $post_id, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) ); >> > if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) < $id3data = wp_get_attachment_metadata( $post_id ); if ( ! is_array( $id3data ) ) < $id3data = array(); >foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) < if ( isset( $post_data[ 'id3_' . $key ] ) ) < $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) ); >> wp_update_attachment_metadata( $post_id, $id3data ); > // Meta stuff. if ( isset( $post_data['meta'] ) && $post_data['meta'] ) < foreach ( $post_data['meta'] as $key =>$value ) < $meta = get_post_meta_by_id( $key ); if ( ! $meta ) < continue; >if ( $meta->post_id != $post_id ) < continue; >if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_id, $meta->meta_key ) ) < continue; >if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_id, $value['key'] ) ) < continue; >update_meta( $key, $value['key'], $value['value'] ); > > if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) < foreach ( $post_data['deletemeta'] as $key =>$value ) < $meta = get_post_meta_by_id( $key ); if ( ! $meta ) < continue; >if ( $meta->post_id != $post_id ) < continue; >if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_id, $meta->meta_key ) ) < continue; >delete_meta( $key ); > > // Attachment stuff. if ( 'attachment' === $post_data['post_type'] ) < if ( isset( $post_data['_wp_attachment_image_alt'] ) ) < $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); if ( get_post_meta( $post_id, '_wp_attachment_image_alt', true ) !== $image_alt ) < $image_alt = wp_strip_all_tags( $image_alt, true ); // update_post_meta() expects slashed. update_post_meta( $post_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); >> $attachment_data = isset( $post_data['attachments'][ $post_id ] ) ? $post_data['attachments'][ $post_id ] : array(); /** This filter is documented in wp-admin/includes/media.php */ $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data ); > // Convert taxonomy input to term IDs, to avoid ambiguity. if ( isset( $post_data['tax_input'] ) ) < foreach ( (array) $post_data['tax_input'] as $taxonomy =>$terms ) < $tax_object = get_taxonomy( $taxonomy ); if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) < $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) ); > > > add_meta( $post_id ); update_post_meta( $post_id, '_edit_last', get_current_user_id() ); $success = wp_update_post( $translated ); // If the save failed, see if we can sanity check the main fields and try again. if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) < $fields = array( 'post_title', 'post_content', 'post_excerpt' ); foreach ( $fields as $field ) < if ( isset( $translated[ $field ] ) ) < $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] ); > > wp_update_post( $translated ); > // Now that we have an ID we can fix any attachment anchor hrefs. _fix_attachment_links( $post_id ); wp_set_post_lock( $post_id ); if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) < if ( ! empty( $post_data['sticky'] ) ) < stick_post( $post_id ); >else < unstick_post( $post_id ); >> return $post_id; >