Try to use WP_Query
instead of using the SQL
. You can get the reference here.
Simple example would be like:
$args = array(
'post_type' => 'my_custom_post_type',
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date()
)
)
);
$query = new WP_Query($args);
and use a simple while
loop to iterate through the results.
See this for more info on meta query parameters.