Ask your WordPress questions! Pay money and get answers fast! (more info)

How do I group and sort Posts by same custom field value

  • SOLVED

Hello WP Experts!

I have a custom post type named Events and each of the posts under "Events" has the custom field 'start date' and 'end date'. I have no problem in gathering the data, my problem is displaying the posts such as they are grouped according to date (daily).

My question is, how do I sort the posts according to the same date so that they would have this following output.

Example output:

<strong>October 1, 2011</strong>

1. WordPress Summit
2. Another Event

<strong>October 2, 2011</strong>

1. WordPress Summit

<strong>October 3, 2011</strong>

1. Rock Event Concert
2. Another Event for this day
3. Final Event for this day
4. WordPress Summit

Notice 'WordPress Summit' appears on all the dates because that event happens on all those dates.

I hope somebody out there could help me out on this problem. I've been going through this for weeks and I decided to ask for help on the community. Thanks y'all.

Answers (3)

2011-10-19

Mines answers:

Try this code. There was a typo in the previous one:
(New edit)


if (0 < count($events = get_posts(array('order' => 'ASC', 'numberposts' => '-1', 'post_type' => 'events')))) {
$ordered_events = array();
$decade = '20';
foreach ($events as $event) {
if ('' != $event_meta = trim(get_post_meta($event->ID, 'event-date-start', true))) {
list($year, $month, $day) = explode("/", $event_meta);
$timestamp = mktime(0, 0, 0, $month, $day, $decade . $year);
$ordered_events[$timestamp][] = $event;
}
}
$timestamps = array_keys($ordered_events);
ksort($timestamps);
foreach ($timestamps as $timestamp) {
echo '<h3>' . date('F j, Y', $timestamp) . '</h3>';
echo '<ul>';
$events = $ordered_events[$timestamp];
foreach ($events as $event) {
echo '<li>' . $event->post_title . '</li>';
}
echo '</ul>';
}
}


Dino Latoga comments:

yes, that works. in fact i've already tried that before but that is not what i am looking for. the posts should be grouped by date.

anyway, thanks. :)


Dino Latoga comments:

date format that i am using right now is yy/mm/dd but i am always open to suggestions and can change the format to which can work best.


Dino Latoga comments:

Thanks for the code but I've tried it and it does not work properly. But you have the right idea about what I want already.

It outputs the date as header but the list is empty - it does not display any posts at all.

My actual setup is

post type: event
start date : event-date-start
end date : event-date-end

I want to display the events like this:

September 28, 2011
1. Cat's Birthday
2. Musical Event

September 29, 2011
1. WordPress Event
2. Q&A with Matt
3. Test Event

September 30, 2011
1. WordPress Event (same event as above)

I hope you get the idea. Looking forward to your next reply.



Dino Latoga comments:

Hi Mines,

It's working now. The grouping is correct but the date heading is not changing. Here is an example output so you know what I mean.

2011-09-28

An Event header

2011-09-28

Lorem ipsum dolor sit amet

2011-09-28

Curabitur id dolor
Mauris placerat auctor

2011-09-28

John Doe- Whispering Jack 25th Anniversay

2011-09-28

Doctor Zhivago

2011-09-28

The WordPress Project

2011-09-28

Climate Change Forum

So as you can see, the date heading is not changing.

2011-10-19

Ivaylo Draganov answers:

Hello,

here's my first stab at solving the problem:
[[LINK href="http://pastebin.com/2nS5xvQj"]]http://pastebin.com/2nS5xvQj[[/LINK]]

It groups events by the value of the custom field 'start date'. It's nowhere near ready... but it's a start. Dealing with events that span more than one day would be more tricky though.

<strong>Something very important:</strong> In what format are you storing the date values?


Dino Latoga comments:

the date values are stored in yy/mm/dd format but since this is still on development i can always change it. what do you think may be the best format that i should use?


Ivaylo Draganov comments:

I'd suggest sticking to standards when it comes to storing date/time data. Take a look at [[LINK href="http://en.wikipedia.org/wiki/ISO_8601"]]ISO 8601[[/LINK]]. This standard uses the format <em>YYYY-MM-DD</em>.

I've made some tweaks to my function above but I couldn't come up with a way to handle multi-day events.

2011-10-19

Romel Apuya answers:

this would do it

<?php $events = get_posts(array('order' => 'DESC', 'numberposts' => '-1', 'post_type' => 'events', 'meta_key' => 'start date', 'orderby' => 'meta_value&meta_key=start date')); ?>