• Resolved sweden1

    (@sweden1)


    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 3 years, 1 month ago by sweden1.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator threadi

    (@threadi)

    What do you mean by

    not for complex strings like long WP-posts that were modified by a program

    Do you have a concrete example for this?

    Thread Starter sweden1

    (@sweden1)

    @threadi, thank you for your interest

    The texts are under work and can not be put here, unfortunately. Here is a different situation for another person, but with some similarity

     the query works when I replace the first two lines with hard-coded values,

    https://stackoverflow.com/questions/26924553/cant-get-variables-to-work-in-wpdb-update-query

    So right now I can not go into analysis of the input. I had string variables (blog posts) read by get_post() (and probably modified by preg_replace()) that would not store with wp_update_post(). Nothing happened. (But I could write them as text or html files.) But if I replaced them with string constants, they stored. I hope I can come back to this when I have the time to analyze the input. So there is no way to simply “sanitize” a string so it will always store with wp_update_post() (at least that was my inpression of the problem)?

    So my question can only be more limited right now. Has anyone else experienced this and solved it? It is a simple problem: Read a post, change it and update it. Is there any other approach than the ones I mentioned, that so far failed for me?

    Right now I have decided to try replace/update in SQL. As I understand only mySQL 8 has regex and GoDaddy doesn’t support it on shared hosting, so I will be without regex, capture groups etc. I must have good backup since I can not rollback changes. I first loop through the posts using php and preg_match_all() to get an impression of what the substitutions using mySQL will do.

    Thank you for your interest. Sorry I can not go into the detail of the blog posts that wouldn’t store.

    • This reply was modified 3 years, 1 month ago by sweden1.
    • This reply was modified 3 years, 1 month ago by sweden1.
    Moderator threadi

    (@threadi)

    It doesn’t have to be exactly one string that you use. You can also anonymize it, e.g. with lorem ipsum words. I guess the decisive factor is whether the text is a string and whether and which special characters it may contain.

    Furthermore, it would be interesting to know in which context you perform this update. Is this an API access, a cronjob or some hook triggered by a user?

    Without such information it is difficult to help you.

    Thread Starter sweden1

    (@sweden1)

    @threadi

    I just run php code from a WP page using a plugin that makes that possible. I am only making changes that will happen once to update some references in the text of some posts.

    I am very satisfied with your help so far. I must be more specific and analyze the input better before coming back with this question. So I put solved on this question since I now know how to proceed. I also got good information that there doesn’t seeem to be any general limitation with wp_update_post(). This is the way to go (get_post() and wp_update_post() ). So I will continue on this in due course with a new question that will be more specific.

    Thank you for your advice for the time being.

    Thread Starter sweden1

    (@sweden1)

    in which context you perform this update. Is this an API access, a cronjob or some hook triggered by a user?

    @threadi

    I can just say your advice was very good. The problem probably originated in using a plugin that was fine for its purpose of introducing php snippets, but not for developing programs. I made a plugin of the code instead and several problems disappeared.

    I don’t really know which is the best way to write a program that will be run occasionally to scan the database. I prefer to run on the server since I sometimes must change programs just from a phone. I also get automatic backup of the installation. Now I just activated the plugin from a shortcode so I didn’t have to go through constructing a menu and admin page for the plugin. No other users have writing access to the installation.

    After changing the program to a plugin, I couldn’t reproduce any errors of strings that could not be written to posts.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to edit post content using php’ is closed to new replies.