Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Dennis Eitner
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Eitner
Full Stack JavaScript Techdegree Graduate 25,643 Points

How do I apply a sort order to a wordpress loop if the sortby is a custom field (acf) and has the format "dd.mm"?

I am trying to get post order to be sorted by date (custom field). But when I apply sort order, it does sort but only by days, not by date.

$dateformatstring = "m.d.y"; $date = strtotime(get_field('datum'));

$args = array ('post_type' => 'events', 'meta_key' => $date, 'orderby' => 'meta_value_num', 'order' => 'ASC' );

2 Answers

Can you explain further on your question. so you are trying to sort it by dates and days. Do you want the date and days to appear on the post? is thats the case you can use $latest and then pull out the publish information associated with the post. if l have totally get it all wrong then do explain further.

Also i would say read the wordpress codex and use less PHP hard code in your wordpress dev as you would make things harder for yourself. Use the appropriate wordpress php functions and that.

Dennis Eitner
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Eitner
Full Stack JavaScript Techdegree Graduate 25,643 Points

Hi Kevin. Thanks for your answer. I would like to sort a custom post type with custom fields by date. I have the following fields:

Date of event Time Title The Content.

The tricky part for me is to get the right sort order for the date format. I (european date: dd.mm). Right now it sort only for the days. So 29.02 is higher than 05.04. I am a little stuck here.

Thanks for you help.