Converting MYSQL to WordPress $WPDB

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Converting MYSQL to WordPress $WPDB



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. 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.



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++;
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

tUq0v,hHbm,7kZtij,7VNrsmy0GAkr3i71P
0JBScwaMZ ZHAKF,AMMq3 vN4T

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham