How to edit post content using php

[ad_1]

I wish to take out WordPress post content from the database, modify it using a php program and insert it back into the database. I have tried different ways.

1. Using WP routines:

$my_post = get_post( $id );
$my_post->post_content = $content;
wp_update_post( $my_post );

However, wp_update_post( ) will only work for simple contants like “This is the new content” and not for complex strings like long WP-posts that were modified by a program. There also seems to be some formatting included in $my_post->post_content, it is not the content straight out of the database.

2. using mySQL routines

SELECT post_title, post_content FROM wp_posts WHERE ID= ..."
$sql = "UPDATE wp_posts SET post_content="" . $content . "" WHERE ID=...";

This will give the actual content of the database but it has been difficult to update the database with long and complex strings like entire blog posts. This also only works for simple strings.

So to get further on how to modify blog posts using php I please ask here. I have considered the following

Using phpMyAdmin straight into the database. GoDaddy web host does not support MySQL 8 which is needed for regex to work in SQL-statements using replacements as I understand, but there is box for regex you can tick in a replace function. It will be less flexible than php, e.g. you can not direct it to skip certain posts.

It would be possible to study how WP stores the posts when you dump the database in text and make a similar program. But that would require more time than I have available.

I have been looking for routines to prepare post content before inserting it into the database, but none have worked. One thing is naturally how to treat the quote characters. WP itself seems to change quote characters in its internal representation.

So maybe I have made this a little too complicated. How can you just extract the WP post content from the database, modify it in a php program and put it back into the database and replace the old post?

  • This topic was modified 13 hours, 31 minutes ago by sweden1.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer