How to Edit Post Content Programmatically in WordPress
X

Congrats, You are Subscribed to Receive Updates.

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.

<?php 

//here you need to specify the post id in-order to get the post to edit
$kv_post = $wpdb->get_row("SELECT post_content,post_title FROM $wpdb->posts WHERE ID = 13");

//get 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.

commenter

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, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

2 comments

  1. commenter

    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!

    • commenter

      Hi wil,
      this is a sample snippet to edit a post through programmatically, and there you see I queried with a post ID = 13. 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 .

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts