1

I am trying to create a customized query that would result in the following.

-- Monday, January 5, 2018 --

This is a post title from 01-05-2018

This is a post title from 01-05-2018

This is a post title from 01-05-2018

-- Tuesday, January 6, 2018 --

This is a post title from 01-06-2018

This is a post title from 01-06-2018

This is a post title from 01-06-2018

-- Wednesday, January 7, 2018 --

This is a post title from 01-07-2018

This is a post title from 01-07-2018

This is a post title from 01-07-2018

... and so on.

I have created a query args with the following:

$args = array(
        'post_type' => 'show',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'   => 'meta_value',
        'meta_key'  => 'show_date',
        'order' => ASC
    );

Which gives me the posts I need but is there a way to inject the date in between each day?

/TIA

  • 2
    You cant solve this with a query only, you need to loop through the posts, check their date and breakt it up accordingly – funkysoul Feb 14 '18 at 12:56
  • Is the date the date the post was made, or the custom field? If it's the meta value, in what format is the date stored (will help with answering)? – Jacob Peattie Feb 14 '18 at 13:06
  • It is a custom field. "show_date" – Matthew Woodard Feb 14 '18 at 13:07
  • 1
    Would it not make more sense to use the posts date for the post date? Any issues with visibility can be fixed with the pre_get_posts filter, and it would be faster than ordering by a meta value – Tom J Nowell Feb 14 '18 at 13:17
  • 1
    You're not going to be able to sort by F j, Y. To sort by date you'll need to store it in a format MySQL can understand, like YYYY-MM-DD which is Y-m-d. Then you can order by setting meta_value_type to DATE and orderby to meta_value_date. – Jacob Peattie Feb 14 '18 at 13:37
3

I was able to do this using the following:

$args = array(
    'post_type' => 'show',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby'   => 'meta_value',
    'meta_key'  => 'show_date',
    'order' => ASC
);

$query = new WP_Query( $args );

if( $query->have_posts() ) {
        $day_check = '';
        while( $query->have_posts() ): $query->the_post();

            $day = date('Y-m-d', strtotime(get_field('show_date')));

            if ($day != $day_check) {
                if ($day_check != '') {
                  $output .= '</ul>'; // end ul.shows
                }

                $output .= '<h3 class="date">' . date('l, F jS, Y', strtotime(get_field('show_date'))) . '</h3>';
                // start this date's show list
                $output .= '<ul class="shows">';
            }

            // what ever you want to show from the posts
            $output .= '<li>' . get_the_title() . '</li>';

        $day_check = $day;

        endwhile;
        wp_reset_postdata();
}
| improve this answer | |
  • 1
    upvoted for answering own question :) – Tom J Nowell Feb 14 '18 at 16:16

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.