1

I have a custom post type where all titles are dates (in this format: YYYY-MM-DD). I need to order them by month then day, but not at all by year. So I imagine I need a filter to 'look' 5 characters into the title and sort by that. The results I'm looking would be like this:

  • 2015-01-01
  • 2018-01-28
  • 2017-05-31
  • 2018-06-14
  • 2014-06-21

These posts have corresponding ACTUAL dates via Advanced Custom Fields datepicker so it may be easier to use that instead of the title. I've tried to figure it out both ways to no success. I'd be happy with a solution either way.

0

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.

5
  • 1
    This seems to be sorting posts by the post date, which I believe is the WP default. The only thing I've changed are the $args (below) and I put the_title(); echo "<br>"; where you have // Show posts. $args = array( 'post_type' => 'z_day', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'day_add_almanac', 'value' => 'yes' ) ) ); Mar 3 '19 at 0:29
  • @user2215732 Thanks for pointing out the mistake. Check now . I have updated my answer. Let me know if it works now? Mar 3 '19 at 1:42
  • sorry, no. It's doing something now but the sort order is neither default nor what I'm looking for. I can't make sense of the sort. Here's a sample of the results produced: 2018-09-14, 2017-11-15, 2017-10-25, 2017-10-16, 2018-09-01, 2018-06-14, 2018-03-01, 2018-04-01, 2018-04-28, 2017-09-27 Mar 3 '19 at 19:09
  • @user2215732 please re-check your code once again, it works without fail on my setup. Mar 3 '19 at 19:47
  • @user2215732 Here is quick demo: test.attocknet.com/post-order-test on my setup. Created a shortcode from the code and uploaded to server. Mar 3 '19 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.