I am trying to display a particular value from the WordPress database for a specific ID with no such luck. I am not receiving any errors, but just an empty result. Here is my table:

table name: _customtable

id     item_key     item_value
1      url          https://mywebsite.com
2      techurl      https://awsinstnaceid.production.mywebsite.com

Here is the code I'm trying to use to display the techurl value in item_value

global $wpdb; 
$table_name = $wpdb->_customtable; 
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name WHERE 'id'= 2" ); 
foreach ($retrieve_data as $retrieved_data) {  
echo $retrieved_data->item_value;  } 

You have 2 two problems here: - You can't use $wpdb-> with custom tables because [postmeta, posts, ...] are variables in the class $wpdb. - WHERE 'id'= 2 is not correct WHERE id = 2

So it should work:

global $wpdb;
$table_name = "_customtable";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name WHERE id = 2" );
foreach ($retrieve_data as $retrieved_data) {
    echo $retrieved_data->item_value;  }


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.