Home › Forums › General Issues › Order by Date pick
I have Knowledge in php and i need to change this code:
<?php $catPost = get_posts(array('category'=>get_cat_ID( '' ),'numberposts'=>12)); ?>
<?php foreach ($catPost as $post) : setup_postdata($post); ?>
<?php the_title (); ?>
<?php endforeach; ?>
who show me the last post added.
i want it to show me the post with the latest date in the date picker costum field.
there is a simple way to do that?
Hi @yortem
To order posts via a custom field value, please see:
http://www.advancedcustomfields.com/resources/how-to/orde-posts-by-custom-fields/
Please make sure your date picker has a save format of ‘yymmdd’ or else the order will not work correctly.
Thanks
E
can you please give me example of the code if my field call “field_name”?
i have really low knowledge in php.
Hi @yortem
I have taken the following code straight from the docs above and changed to the field name required:
<?php
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
if( $posts )
{
foreach( $posts as $post )
{
// ...
}
}
?>
Hope that helps.
Thanks
E
thanks. it gave me the the posts by the right field. but the result wasnt what i looked for.
it gave me:
30/12/12
29/11/10
28/04/13
27/02/02
26/11/06
see? by the numbers of the day in the month only not by the real date.
i need something like that:
28/04/13
30/12/12
29/11/10
26/11/06
27/02/02
by the real date.
(sry for my english)
Hi @yortem
Please make sure your date picker has a save format of ‘yymmdd’ or else the order will not work correctly.
i changed to yymmdd
but now its show me 20131119 for example.
my knowledge in php isnt so good, where i put the code that change the display date?
Hi @yortem
When looping through your post results, for each post, you can load the date value, and then format it using the code examples on this doc:
http://www.advancedcustomfields.com/resources/field-types/date-picker/
Thanks
E
The topic ‘Order by Date pick’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.