How to process a single attribute in a SQL request twice times?

I have created a shortcode with only ONE attribute, but the SQL request needs TWO attributes for %s, because of the subquery. The following SQL does not work, because the second %s does not have a value.

When I create a workaround with a second attribute, the second %s in SQL subquery works as expected, but I don’t want to use two identical attributes.

How can I duplicate the single attribute for using it twice in SQL request?

<?php
// Shortcode: [my_shortcode market=”USA”]
function showSomething( $country ){
$country = shortcode_atts( array(
‘market’ => ”,
), $country);

global $wpdb;

$sql = $wpdb->get_results( $wpdb->prepare(
“SELECT `Date`, `Field1`, `Field2`, `Field3`
FROM `table`
WHERE `country` = %s
AND `Date` = (SELECT DISTINCT `Date`
FROM `table`
WHERE `country` = %s
ORDER BY `Date` DESC
LIMIT 1)”, $sql ) );

….
return ….
}
add_shortcode(‘my_shortcode’, ‘showSomething’);

Read more here:: How to process a single attribute in a SQL request twice times?

Leave a Reply

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