WordPress WP_Query’s orderby
parameter has a lot of options but nothing as specific as what you are after.
I think you’d have to do this after retrieving posts through the WP_Query
, then sort them using usort()
.
Your code should be something like this.
$args = array(
'post_type' => 'z_day',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'day_add_almanac',
'value' => 'yes'
)
)
);
// Create object
$my_posts = new WP_Query( $args );
// Sort
usort( $my_posts->posts, function( $a, $b ) {
// Convert "2018-01-28" to "0128"
$c = str_replace('-', '' ,substr($a->title , 5));
$d = str_replace('-', '' ,substr($b->title , 5));
if( $c == $d ) {
return 0;
}
// Use < or > (or swap -1 and 1 ) for reversing order
return ( $c > $d ) ? -1 : 1;
});
// Loop
if( $my_posts->have_posts() ) {
while( $my_posts->have_posts() ) {
$my_posts->the_post();
// Show posts
the_title();
}
}
You may now adjust the above code to meet your requirements.
I hope this helps.