1

I have a wp_query which lists posts from a custom post type, sorted by a custom field 'time'.

I'd like to group this list by another custom field 'date', so that every post that has the same custom field 'date' is listed beneath that date. I do not know these dates in advance.

The output would be like:

Date x
- post 3
- post 6
- post 8

Date y
- post 1
- post 4

Date z
- post 2
- post 5
- post 7
- post 9

Below is my current WP_Query:

<?php
        // WP_Query arguments
        $args = array (
            'post_type'              => 'movie',
            'posts_per_page'         => '50',
            'orderby'                => 'meta_value_num',
            'order'                  => 'ASC',
            'meta_key'               => 'time',
        );

        // The Query
        $query = new WP_Query( $args );

        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                echo "<li><a href='".get_permalink()."'>";
                echo "<div class='xyz'>".get_the_title()."</div>";
                echo "</a></li>";

                $vbrand = get_post_meta( get_the_id(), 'brand', true ); 
                $vyear = get_post_meta( get_the_id(), 'year', true ); 
                $vtime = get_post_meta( get_the_id(), 'time', true ); 

                        echo "<p>".$vbrand;
                echo "<p>".$vyear;
                echo "<p>".$vtime;
            }

        } else {
            // no posts found
        }
        /* Restore original Post Data */
        wp_reset_postdata();
?>
  • What's the date format of the "time" custom field? Is it a UNIX timestamp or something else (e.g. Y-m-d H:i)? – engelen Jun 23 '14 at 7:32
  • It's Y-m-d with todays date being 2014-06-23 – BoBoz Jun 23 '14 at 12:06
  • The dates are 5 different playdates so perhaps it easier to just set a value, like 1 to 5 and group by that value? – BoBoz Jun 23 '14 at 18:38
  • Also just saw kovshenin.com/2012/order-by-post-meta-casted-date-wp_query which says it's hard working with date values in post meta. – BoBoz Jun 23 '14 at 18:39
2

A good approach to this would be to first fetch all posts sorted by date, and then group them.

As you state the time custom field is in the format Y-m-d, so you can properly sort your posts using orderby => 'meta_value' (not meta_value_num, as it currently is, which casts the value to a numeric type).

Next, if you want to output a title for each different date, you can store the last displayed date and output it if the date (and change the stored date) if the next post differs from that date. This would yield something similar to this:

$dategroup = '';

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        // Fetch date for current post
        $postdate = get_post_meta( get_the_ID(), 'time', true );

        // Check if the post date differs from the current date
        if ( $postdate != $dategroup ) {
            // In that case, this is a new date that hasn't yet been displayed
            // So, we output the title...
            echo '<h3>' . $postdate . '</h3>';

            // ...and change the stored date for the current group
            $dategroup = $postdate;

            [your output stuff]
        }
    }
}
  • Hey thanks! Went for setting a value 1 to 5 representing the dates. Also I misunderstod you above. Date is Y-m-d (not numerical) and time is 24 hours format (numerical). – BoBoz Jun 27 '14 at 15:32

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.