The SQL you want would be:
$sql = "
SELECT DISTINCT post_id
FROM $wpdb->posts, $wpdb->postmeta
where $wpdb->postmeta.meta_value="2015/06/03"";
$results =$wpdb->get_results($sql);
But there is no reason to be involving the $wpdb->posts
table at all, since the post_id
value is a $wpdb->postmeta
table column.
$sql = "
SELECT DISTINCT post_id
$wpdb->postmeta
where $wpdb->postmeta.meta_value="2015/06/03"";
$results =$wpdb->get_col($sql);
Note that I changed $wpdb->get_results
to wpdb->get_col
as you are only returning a single column of data. The results will come out needing less manipulation afterward.
You could also skip the DISTINCT
and use PHP’s array_unique()
:
$sql = "
SELECT post_id
$wpdb->postmeta
where $wpdb->postmeta.meta_value="2015/06/03"";
$results =$wpdb->get_col($sql);
$results = array_unique($results);
However, the far more correct way would be to use WP_Query
. I was going to suggest that but then noticed that you are not searching for a meta_key
at all, but are searching the whole table. That is going to cause trouble eventually. Suppose some plugin stores data in the $wpdb->postmeta
table with that date format? Then your code will retrieve those values as well, which is certainly not intended behavior. Someone could even enter such dates as a custom field right from the stock post edit panel. You should specify a key, something like:
$args = array(
'fields' = 'ids',
'ignore_sticky_posts' => true,
'meta_query' => array(
array(
'key' => 'my_date_key',
'value' => '2015/06/03',
)
)
);
$pids = new WP_Query($args);