When I post a post to my site though webapi,there already had a same post_title in my post list, So I just want to update my post_content or replace all data to the old post but not let wp create a new post which post_name became post_name-1.

I got this code with wpdb, but it's not work, please help me, thank you guys!

$checkTitle = true;    
        $post_title = trim(hm_strip_slashes($post_title));
        $sql = "SELECT `ID` FROM $wpdb->posts WHERE `post_title` = '$post_title'";
        $t_row = $wpdb->query($sql);
        if($t_row) {$wpdb->get_results("update $wpdb->posts set `post_content` = $post_content WHERE `post_title` = $post_title");};
| improve this question | | | | |
  • Try taking the single quotes around '$post_title' off. Single quotes tell PHP to read a string literally instead of parsing it and applying the variable. – WebElaine May 15 '19 at 16:01
  • You need $wpdb->update not get_results. But this feels wrong: where have you put this code? Which is the web API you're using - the REST API, POST /wp/v2/posts? Are you modifying the handler for that to do the overwrite? Could you change your client to make an update call instead? – Rup May 15 '19 at 16:15
  • I get this form someone, this code is for a client which can post blog from localhost.I can upload this post.php in root of my wordpres site – Anrufen May 16 '19 at 1:45
  • download here send.firefox.com/download/2bd675a86da59f4f/… – Anrufen May 16 '19 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.