Skip to content

Instantly share code, notes, and snippets.

@j4ckth3r1pp3r
Created November 30, 2017 10:09
Show Gist options
  • Save j4ckth3r1pp3r/9c82a50b62434d5352acce0d2d6c7f7e to your computer and use it in GitHub Desktop.
Save j4ckth3r1pp3r/9c82a50b62434d5352acce0d2d6c7f7e to your computer and use it in GitHub Desktop.
wp_insert_rows not my function, but "wp_update_postmeta_rows" is mine. wp_update_postmeta_rows allows you to bulk add or update postmeta with one sql request
<?php
/**
* A method for inserting multiple rows into the specified table
* Updated to include the ability to Update existing rows by primary key
*
* Usage Example for insert:
*
* $insert_arrays = array();
* foreach($assets as $asset) {
* $time = current_time( 'mysql' );
* $insert_arrays[] = array(
* 'type' => "multiple_row_insert",
* 'status' => 1,
* 'name'=>$asset,
* 'added_date' => $time,
* 'last_update' => $time);
*
* }
*
*
* wp_insert_rows($insert_arrays, $wpdb->tablename);
*
* Usage Example for update:
*
* wp_insert_rows($insert_arrays, $wpdb->tablename, true, "primary_column");
*
*
* @param array $row_arrays
* @param string $wp_table_name
* @param boolean $update
* @param string $primary_key
* @return false|int
*
*/
function wp_insert_rows($row_arrays = array(), $wp_table_name, $update = false, $primary_key = null) {
global $wpdb;
$wp_table_name = esc_sql($wp_table_name);
// Setup arrays for Actual Values, and Placeholders
$values = array();
$place_holders = array();
$query = "";
$query_columns = "";
$query .= "INSERT INTO `{$wp_table_name}` (";
foreach ($row_arrays as $count => $row_array) {
foreach ($row_array as $key => $value) {
if ($count == 0) {
if ($query_columns) {
$query_columns .= ", " . $key . "";
} else {
$query_columns .= "" . $key . "";
}
}
$values[] = $value;
$symbol = "%s";
if (is_numeric($value)) {
if (is_float($value)) {
$symbol = "%f";
} else {
$symbol = "%d";
}
}
if (isset($place_holders[$count])) {
$place_holders[$count] .= ", '$symbol'";
} else {
$place_holders[$count] = "( '$symbol'";
}
}
// mind closing the GAP
$place_holders[$count] .= ")";
}
$query .= " $query_columns ) VALUES ";
$query .= implode(', ', $place_holders);
if ($update) {
$update = " ON DUPLICATE KEY UPDATE $primary_key=VALUES( $primary_key ),";
$cnt = 0;
foreach ($row_arrays[0] as $key => $value) {
if ($cnt == 0) {
$update .= "$key=VALUES($key)";
$cnt = 1;
} else {
$update .= ", $key=VALUES($key)";
}
}
$query .= $update;
}
$sql = $wpdb->prepare($query, $values);
if ($wpdb->query($sql)) {
return true;
} else {
return false;
}
}
/**
* A method for inserting multiple rows into the postmeta table
* Add or update if meta exists
*
* Usage Example for insert:
*
* $insert_arrays = array();
* foreach($assets as $asset) {
* $time = current_time( 'mysql' );
* $insert_arrays[] = array(
* 'post_id'=> 23,
* 'meta_key' => "_thumbnail_id",
* 'meta_value' => "30");
*
* }
*
*
* wp_update_postmeta_rows($insert_arrays);
*
*
* @param array $row_arrays
* @return false|int
*
*/
function wp_update_postmeta_rows($row_arrays = array()) {
if (empty($row_arrays)) return false;
global $wpdb;
$ids_to_delete = array_map(function($array) {
return $array['post_id'];
}, $row_arrays);
$ids_to_delete = implode(', ', array_unique($ids_to_delete));
$meta_to_delete = array_map(function($array) {
return $array['meta_key'];
}, $row_arrays);
$meta_to_delete = array_unique($meta_to_delete);
$meta_name = '';
$i = 0;
foreach ($meta_to_delete as $key) {
if ($i) {
$meta_name .= ' OR ';
}
$meta_name .= "`meta_key` LIKE '{$key}'";
if ($i === 0) $i = 1;
}
$query = "SELECT meta_id FROM `$wpdb->postmeta` WHERE `post_id` IN ( {$ids_to_delete} ) AND ({$meta_name})";
$meta_ids = $wpdb->get_results($query, ARRAY_A);
if (!empty($meta_ids)) {
$i = 0;
foreach ($meta_ids as $meta_id) {
if (isset($row_arrays[$i])) {
$row_arrays[$i]['meta_id'] = $meta_id['meta_id'];
}
$i++;
}
unset($meta_ids, $i);
}
foreach ($row_arrays as &$row) {
if (!isset($row['meta_id'])) $row['meta_id'] = null;
}
unset($row);
$query = "DELETE FROM `$wpdb->postmeta` WHERE `post_id` IN ( {$ids_to_delete} ) AND ({$meta_name})";
// return $query;
$wpdb->query($query);
return wp_insert_rows($row_arrays, $wpdb->postmeta, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment