I’m upgrading an archaic Wordpress site from 3.7. It has some SQL query requests that don’t function after 3.9+ where you now have to connect to the DB via $WPDB (https://codex.wordpress.org/Class_Reference/wpdb). I've taken an initial stab at it but am hung up on the mysql_fetch_array part and also unclear if what I've already done is even correct. Seems like the mysql_fetch_array needs to use get_results (https://developer.wordpress.org/reference/classes/wpdb/get_results/).
I'm fully aware that this is a less than ideal way to approach this and fully intend to modernize everything but in keeping a long story short it would be very helpful to be able to sort this out within this old jumbled paradigm if at all possible.
Attached is the original code and then my attempt (also added a v2 attempt). Thanks for taking a look.
SQL:
$sql = "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = '620' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no";
$result = mysql_query($sql) or die('Content was not loaded. Please refresh your page.');
$counter = 0;
while($post = mysql_fetch_array($result)) {
$sqlSecond = "SELECT meta_value FROM wp_postmeta WHERE meta_id = '".$post['value']."'";
$result1 = mysql_query($sqlSecond) or die('Content was not loaded.');
while($post1 = mysql_fetch_array($result1)) {
$data[$counter] = $post1['meta_value'];
$counter++;
}
}
// get image
$sql = "SELECT meta_value FROM wp_postmeta WHERE post_id = '".$data['1']."' AND meta_key = '_wp_attached_file'";
$result = mysql_query($sql) or die('Content was not loaded.');
$counter = 0;
while($post = mysql_fetch_array($result)) {
$imgURL = $post['meta_value'];
}
WPDB v1:
global $wpdb;
$sql = $wpdb->prepare( "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = '620' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no" );
$result = $wpdb->query ( $sql ) or die('Content was not loaded.');
$counter = 0;
while($post = mysql_fetch_array($result)) {
$sqlSecond = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post['value'] );
$result1 = $wpdb->query ( $sqlSecond ) or die('Content was not loaded.');
while($post1 = mysql_fetch_array($result1)) {
$data[$counter] = $post1['meta_value'];
$counter++;
}
}
// get image
$sql = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE post_id = %s AND meta_key = '_wp_attached_file'", $data['1'] );
$result = $wpdb->query ( $sql ) or die('Content was not loaded.');
$counter = 0;
while($post = mysql_fetch_array($result)) {
$imgURL = $post['meta_value'];
}
WPDB v2:
global $wpdb;
$sql = $wpdb->prepare( "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = '620' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no" );
$result = $wpdb->get_results ( $sql ) or die('Content was not loaded.');
$counter = 0;
foreach ($result as $post ) {
$sqlSecond = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post->value );
$result1 = $wpdb->get_results ( $sqlTitle ) or die('Content was not loaded.');
foreach ( $result1 as $post1 ) {
$data[$counter] = $post1->meta_value;
$counter++;
}
}
// get image
$sql = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE post_id = %s AND meta_key = '_wp_attached_file'", $data['1'] );
$result = $wpdb->get_results ( $sql ) or die('Content was not loaded.');
$counter = 0;
foreach ( $result as $post ) {
$imgURL = $post->meta_value;
$counter++;
}
get_post_meta
. Alsowpdb
does not returns values that fit intomysql
functions, it returns arrays or objects containing data, WP_Error objects, or error values. There's also no guarantee that ACF still uses awp_acf_values
table. Theor die()
style syntax is super unusual for WP too, I would strongly recommend removing it in favor of conditional checksmysql_query(
withmysqli_query( $wpdb->dbh
Warning: mysqli_query() expects at least 2 parameters, 1 given in ...
Am I not using it correctly?