Shortcode select another dbbase

i need connect another database in same host and return product value(woocomerce). The return is null


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb('user','password','dbname','localhost');

    $result= $mydbTest->get_results("SELECT `meta_value` FROM `wp_postmeta` WHERE 'post_id' = 5338 AND 'meta_key' = '_sale_price'");
    $price = $result[0];
    $price = number_format($price, 2, '.', ',');

    return '$ '.$price;

}

SQL Consult

SELECT `meta_value` FROM `wp_postmeta` WHERE `POST_ID` = 5338 AND `meta_key` = '_sale_price'

Return
enter image description here

Update – my Solution works


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb($userdb,$mypass,$mydb,'localhost');
    $query = "SELECT * FROM wp_postmeta WHERE post_id=608 AND meta_key IN('_sale_price', '_regular_price')";

    $results = $mydbTest->get_results($query, ARRAY_A);
        foreach($results as $result){
        if($result['meta_key'] == '_sale_price'){
            $price= $result['meta_value'];
            $price= number_format($price, 2, '.', ',');
        } else if($result['meta_key'] == '_sale_price'){
            $old_price= $result['meta_value'];
            $old_price= number_format($old_price, 2, '.', ',');
        }
    };

    return '$ '.$price;

}

Leave a Reply

Your email address will not be published. Required fields are marked *