WPDB Insert or if exists Update


I’m not crazy familiar with WPDB or SQL in general but I have a custom table for my project and I’m trying to assign some metadata to it. What I’d “like” to happen is if a row exists, update it and if not insert it. I’ve read both Insert and Update in the WPDB Codex but neither really went into an “either or” situation. I thought i could work with update, so my code so far looks like this:

            'post_id'       => $post_id,
            'item_stock'    => $item_stock
        array('post_id' => $post_id)

Does WordPress have anything like an “IF exists Update, ELSE Insert”, or do I need to run custom SQL to achieve this, or do I need to query the database first to see if an ID exists in my table THEN decide whether to update it or insert it?

, , Howdy_McGee 3 years 2020-08-25T00:10:43-05:00 0 Answers 76 views 0

Leave an answer