i am working on a plugin that includes table in database that holds information from the different posts. right now i use $wpdb->replace(), so whenever a post is being created or updated it is also create a new row in my table or update one that is already exist.

my questions are:

  1. is replace() already sanitizing the values before insert them into my table or do i have to use prepare() before i use replace?
  2. is it ok to use replace so i can cover both cases of "insert" for new post and "upadte" if updating post or should i use update() if exist else "insert" if not. i know that replace is slower when it comes to performance but maybe it is ok when not being used very often (well, only when creating and updating posts)?
  • Please reformat your question to full english (uppercase/lowercase for example). Thanks.
    – kaiser
    May 4, 2012 at 7:49

1 Answer 1


For Ques#1, You don't need to use prepare here. Its called upon your data internally. See this line in code here - http://core.trac.wordpress.org/browser/trunk/wp-includes/wp-db.php#L1220

For Ques#2, Why are you updating a post with sql queries? You should be using WordPress API for that - http://codex.wordpress.org/Function_Reference/wp_update_post

  • thank you for the replay. i guess i wasn't clear enough in my question. i am updating my plugin's table with info from the post. i need the custom fields of the post (street,city,state,zipcode), post id and latitude/longitude that i get from google API. so whenever a user creates a new post with address i need all the above info to go to my table as well as when he updates the post.
    – Eyal
    May 3, 2012 at 20:38
  • Just use a post ID in your custom table, no need to keep the data you already have in another table. This is just redundancy.
    – Ashfame
    May 3, 2012 at 20:41
  • you right about that. i dont need all of the info and i was thinking to remove but i still need some info. i need the post id,lat/long, post type and post status that i didn't mention. i need this info to run a query and display results by distance from an address.
    – Eyal
    May 3, 2012 at 20:53
  • yeah just keep them to minimum. redudancy comes with the extra overhead of keeping multiple copies in sync. Best would be to just store the post ID and nothing else. And even then if you are going to, just update the row. I don't see the need of replacing the row.
    – Ashfame
    May 3, 2012 at 20:57
  • i will not be able to run the query with only id in my table. well, i could do it by joining the post table and meta table and check lat/long which stored in custom fields agains the lat/long of the address that the user enter. which i think that when it comes to performance it is much better to run it on one table. correct me if i am wrong. and i use replace in case that this is a new post and need to create new row in my table. is "update" creating new row if not exist?
    – Eyal
    May 3, 2012 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.