First i write manually update, delete, insert and select query and execute data with mysql_query function

Like this:

Select query

$prefix = $wpdb->prefix;
$postSql = "SELECT DISTINCT post_id
            FROM " . $prefix . "postmeta As meta
            Inner Join " . $prefix . "posts As post
            On post.ID = meta.post_id
            Where post_type = 'product' 
            And post_status = 'publish'
            And meta_key Like '%product_img%'";
$postQry = mysql_query($postSql);
while ($postRow = mysql_fetch_array($postQry)) {
     $post_id = $postRow['post_id'];

Insert Query

$insert_images = "Insert Into " . $prefix . "postmeta(post_id,meta_key,meta_value) Value('$post_id','$meta_key','$data_serialize')";

Update Query:

$update_price = "Update " . $prefix . "postmeta
                         Set meta_key = 'wpc_product_price'
                         Where post_id = $supportMetaID
                         And meta_key Like '%product_price%'";

Delete Query

mysql_query("Delete From " . $prefix . "postmeta Where meta_key IN ('product_img1','product_img2','product_img3')");

All queries are working perfectly ... but now i want to embed all queries in wordpress queries.

I can also use wordpress queries like

$wpdb->get_results( "SELECT id, name FROM mytable" );
        'column1' => 'value1', 
        'column2' => 123 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    array( 'ID' => 1 )
$wpdb->delete( 'table', array( 'ID' => 1 ) );

But you can see that i use and / or conditions in my queries. So any body help me how can i embed my queries in wordpress

  • 3
    WordPress offers $wpdb Class for doing all those. But a query like the first one can easily be achieved with WP_Query() with less effort. May 5, 2015 at 8:18

2 Answers 2



I have made a function for myself, and might help you too, i.e. :

UPDATE_OR_INSERT('wp_users',  array('gender'=>'female'), array('name'=>'Monika') );

that will UPDATE A VALUE in column (where name=monika), but in case that value doesnt exists, then it creates a new record in DB.
Why this is necessary? because As far as i know, there is no sophisticated WP function, that will update data in DB (if value exists) or inserts data (if not exists). Instead, we use : $wpdb->update() or $wpdb->insert(). So, use that function, it helps:

function UPDATE_OR_INSERT($tablename, $NewArray, $WhereArray){          global $wpdb; $arrayNames= array_keys($WhereArray);
    //convert array to STRING
    $o=''; $i=1; foreach ($WhereArray as $key=>$value){ $o .= $key . ' = \''. $value .'\''; if ($i != count($WhereArray)) { $o .=' AND '; $i++;}  }
    //check if already exist
    $CheckIfExists = $wpdb->get_var("SELECT ".$arrayNames[0]." FROM ".$tablename." WHERE ".$o);
    if (!empty($CheckIfExists))  { return $wpdb->update($tablename, $NewArray,  $WhereArray );}
    else                         { return $wpdb->insert($tablename,     array_merge($NewArray, $WhereArray) );  } 

Update Query:

$table = $wpdb->prefix . 'tablename';

$wpdb->update( $table_name, array( 'role_id' => 1),array('user_id'=>$user_id));

Delete Query:

$wpdb->delete( $table, array( 'id' => $id ) );

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.