I am using wordpress Version 4.9.6 and trying to create the following API:
However, when accessing the api via
http://localhost/demo_wordpress_api/wp-json/product/v1/manageproduct?category=computer
Find below my minimum viable example:
<?php
add_action('rest_api_init', 'productRoutes');
function productRoutes()
{
register_rest_route('product/v1', 'manageproduct', array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'allproductitability',
));
}
function allProductsByCategory($data)
{
global $wpdb;
// show db errors
$wpdb->show_errors(true);
$wpdb->print_error();
// $data['term']
$mainQuery = $wpdb->get_results( "SELECT *
FROM wp_product_API
WHERE id IN(
SELECT MAX(id)
FROM wp_product_API
WHERE category = \" " + $data['category'] + " \"
GROUP BY id)
ORDER BY price DESC
LIMIT 1;" );
When I run this I get the following error:
Warning: A non-numeric value encountered
I get the error where I am inserting my parameter into my SQL Query:
$mainQuery = $wpdb->get_results( "SELECT *
FROM wp_product_API
WHERE id IN(
SELECT MAX(id)
FROM wp_product_API
WHERE category = \" " + $data['category'] + " \" // ON THIS LINE I GET THE ERROR
GROUP BY id)
ORDER BY price DESC
LIMIT 1;" );
Any suggestions why I get the error on this line?
I appreciate your replies!