You can’t run arbitrary PHP functions in a query like that. WP_Query
(which get_posts()
uses) doesn’t retrieve posts then filter them with PHP, it gets turned into an SQL query which finds the results.
The proper way to order meta_value
by date in SQL would be to cast it as DATETIME
. The way WP_Query
allows you to do this is by the meta_type
argument. From the documentation:
You may also specify
‘meta_type‘
if you want to cast the meta value as
a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’,
‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as
in‘$meta_query‘
.
In your case you’d use it like this:
$args = array(
'post_type' => 'my_post_type',
'meta_key' => 'date',
'meta_type' => 'DATETIME',
'orderby' => 'meta_value'
'order' => 'ASC',
);
Note that I haven’t tested this, as I don’t have appropriate data to test with.