I'm trying to get post meta to be updated/replaced with $wpdb and running into issues. I have working SQL, but cannot get it work when coding it up for my WP plugin.

Extra note:

The post meta being saved is for example is Twitter follower count that I need the number to be saved to the database so I can export the result in a table

Here is my working SQL. When I put this in PHPmyAdmin it updates all post meta:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT wp_posts.ID, 'agency_details_seo_grade', 'test'
FROM wp_posts
WHERE wp_posts.post_status = 'publish' and wp_posts.post_type = 'agencies'";

Here is my failed attempt putting it in WP. Nothing happened as a result (no meta changed and no error message):

// Failed attempt to put into functions.php
function add_grading_post_meta() {
    global $wpdb;
    $wpdb->prepare("SELECT ID,'','test2'", $wpdb->query("INSERT INTO wp_postmeta ($post_id,meta_key,meta_value)"));

I'm trying to get this working so I can use it with wp_cron before the next step of using it with WP scheduling functions. Any help would be appreciated for this first step, cracking a lot of hours on this project learning PHP.


Here's 1 more version I tried with no success. I changed it due to @czerspalace comment, coding provided by old Stack Exchange post.

function add_grading_post_meta() {
    global $wpdb;

    $twitter_followers = 'agency_details_twitter_followers';
    $twitter_count = 'test3';

    $sql = "INSERT INTO $wpdb->wp_postmeta (post_id,meta_key,meta_value) VALUES   (%d,'%s',%s) ON DUPLICATE KEY UPDATE meta_value = %s";
    $sql = $wpdb->prepare($sql,$post_id,$twitter_followers,$twitter_count);
  • What if you add $wpdb->print_error(); after your prepare statement, what do you see? Your syntax is incorrect. Your $wpdb->query should be outside of prepare Look here for how to use prepare and query: developer.wordpress.org/reference/classes/wpdb/prepare – czerspalace Nov 6 '15 at 23:36
  • @czerspalace I updated with new code. Thanks for pointing that out. – Julian Flynn Nov 7 '15 at 1:54

Your use of $wpdb->prepare is very broken. I honestly don't even know exactly what query you are trying get. The code is so broken I can't even infer what you are trying to do. However, I am guessing you want something like this:

$post_id = 1;
$key = 'abc';
$value = 'def';
$qry = "INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES (%d,%s,%s)";
$qry = $wpdb->prepare(

Notice what happens. You create a "format" string with placeholders for you dynamic data. Then you pass that through prepare along with the replacement strings for the placeholders, and get back a string that is sent through query.

  • Thanks for answer, you're the expert here because it was your posts I was reading and trying to decipher for doing this :) I tried your code above and still didn't see post meta added. Sorry if my 'prepare' code was bad, I posted the SQL though for what I'm trying to do (that worked). Let me know if you need more info if it changes your original answer. – Julian Flynn Nov 7 '15 at 1:48
  • Also to include to my previous message, I try changing 'key' and 'value' after as well and still no luck. – Julian Flynn Nov 7 '15 at 1:57
  • The basic idea doesn't change, the $qry string would be different. – s_ha_dum Nov 7 '15 at 4:04
  • If this was resolved then please accept the answer or write up your own answer to keep the site tidy. Thanks. – Andy Macaulay-Brook Oct 8 '16 at 10:32

Actually, the customary way to do this in WordPress would be to use <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> as per the codex: https://codex.wordpress.org/Function_Reference/update_post_meta. It yields shorter code, and the intent of the code is clearer.

Your Answer

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

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