You can add a custom column with a value to the returned row object from the $wpdb
query in WordPress as follows:
phpCopy codeglobal $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name WHERE column_name = 'value'" );
foreach ( $results as $result ) {
$result->custom_column = 'custom value';
}
In this example, we are using the get_results
method of the $wpdb
object to run a SELECT query. The get_results
method returns an array of row objects, which we can then loop through to add a new column to each row object.
You can then use the custom column in your code as follows:
phpCopy codeforeach ( $results as $result ) {
echo $result->custom_column;
}
Note that this will modify the original row object, so it is important to make a copy of the row object if you want to keep the original values intact.
Last modified: March 3, 2023