I have a custom table 'wp-products'
in WordPress with the following fields
id (primary key) e.g 1
make e.g. ford
model e.g. mustang
price e.g. 17500.00
id (primary key) e.g 2
make e.g. dodge
model e.g. pheonix
price e.g. 77500.00
what I want to do is create a shortcode e.g. [product_price id=2]
that returns the price value associated with id number 2 in this table e.g. 77500.00
. I have tried several ways and ended up with this (below)
I put this code in functions.php
function product_price_func( $atts ) {
global $wpdb;
$output="";
$product_id = '';
$atts = shortcode_atts(
array(
'id' => 'no id found',
), $atts, 'product_price' );
$product_id = $atts['id'];
$product_price = $wpdb->get_results("SELECT price FROM wp-products WHERE id={$product_id}", ARRAY_A);
foreach ( $product_price as $the_price )
{
$output = $the_price->price;
}
return "Product Price: $" . $output;
}
add_shortcode( 'product_price', 'product_price_func' );
However, this returns an empty result. I have checked that it passes id
correctly but cannot get the query to return the value associated with id=2
.