Come utilizzare le query di aggiornamento ed eliminazione in WordPress

5 mag 2015, 09:40:21
Visualizzazioni: 65.9K
Voti: 4

Prima ho scritto manualmente le query di aggiornamento, eliminazione, inserimento e selezione ed ho eseguito i dati con la funzione mysql_query

In questo modo:

Query di selezione

$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'];
}

Query di inserimento

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

Query di aggiornamento:

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

Query di eliminazione

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

Tutte le query funzionano perfettamente... ma ora voglio incorporare tutte le query nelle query di WordPress.

Posso anche usare le query di WordPress come

$wpdb->get_results( "SELECT id, name FROM mytable" );
$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
);
$wpdb->update( 
    'table', 
    array( 
        'column1' => 'value1',  // stringa
        'column2' => 'value2'   // intero (numero)
    ), 
    array( 'ID' => 1 )
);
$wpdb->delete( 'table', array( 'ID' => 1 ) );

Ma come potete vedere, uso condizioni and / or nelle mie query. Quindi qualcuno può aiutarmi a capire come posso incorporare le mie query in WordPress

1
Commenti

WordPress offre la classe $wpdb per fare tutte queste operazioni. Ma una query come la prima può essere facilmente realizzata con WP_Query() con meno sforzo.

Mayeenul Islam Mayeenul Islam
5 mag 2015 11:18:49
Tutte le risposte alla domanda 2
0

Informazioni su UPDATE+INSERT:

Ho creato una funzione per me stesso, che potrebbe esserti utile, ad esempio:

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

Questa funzione AGGIORNERÀ UN VALORE nella colonna (dove name=monika), ma nel caso in cui il valore non esista, creerà un nuovo record nel database.
Perché è necessario? Perché, per quanto ne so, non esiste una funzione sofisticata in WordPress che aggiorni i dati nel database (se il valore esiste) o li inserisca (se non esiste). Invece, usiamo: $wpdb->update() o $wpdb->insert(). Quindi, usa questa funzione, ti aiuterà:

function UPDATE_OR_INSERT($tablename, $NewArray, $WhereArray){          global $wpdb; $arrayNames= array_keys($WhereArray);
    //converti l'array in STRINGA
    $o=''; $i=1; foreach ($WhereArray as $key=>$value){ $o .= $key . ' = \''. $value .'\''; if ($i != count($WhereArray)) { $o .=' AND '; $i++;}  }
    //controlla se esiste già
    $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) );  } 
}
1 set 2016 14:52:01
0

Query di Aggiornamento:

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

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

Query di Eliminazione:

$wpdb->delete( $table, array( 'id' => $id ) );
4 mag 2020 13:20:46