Home › Forums › General Issues › ACF posts orderby not working correct
Hi!
I use ACF to get show a ranking. Every post contains a time which is a nummeric field. Now i get these posts and order them by this field (fieldname contest_endtime). Now when i get these posts the ordering is somehow random. And i dont know what to do.
Thx for your help!
My php
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'contest',
'meta_key' => 'contest_route',
'meta_value' => 'route_small',
'orderby' => 'contest_endtime',
'order' => 'ASC'
));
if( $posts ): ?>
<div class="col"><?php echo get_field("contest_endtime",get_the_ID()); ?></div>
</div>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
The ACF time field stores a value that looks something like this 08:14:10
. Are you expecting posts to be ordered by a data and an time?
No the field is a nummeric field. Just normal nummeric values without any decimals or something.
Please provide more information about the field and how the value stored in it is generated.
The special thing is, the nummeric field is a field which is generated out of 2 date fields. I set the start and the endtime of the run and when i save the post (backend frontend) the field contest_endtime is calculated automatically by a function. This is saved in my functions.php:
function save_contest_post( $post_id ) {
// vars
$fields = $_POST['acf'];
// get custom fields (field group exists for content_form)
$skipper = sanitize_title(get_field('contest_skipper', $post_id));
$yardstick = get_field('contest_yardstick', $post_id);
$startdate = get_field('contest_startdate', $post_id);
$starttime = get_field('contest_starttime', $post_id);
$startdatetime = $startdate .' '. $starttime;
$changetime = get_field('contest_changetime',$post_id);
$changedatetime = $startdate .' '. $changetime;
$goaltime = get_field('contest_goaltime',$post_id);
$goaldatetime = $startdate .' '. $goaltime;
$startdatetime = date_create_from_format('d.m.Y H:i:s', $startdatetime);
$changedatetime = date_create_from_format('d.m.Y H:i:s', $changedatetime);
$goaldatetime = date_create_from_format('d.m.Y H:i:s', $goaldatetime);
$interimTime = ($changedatetime->getTimestamp() - $startdatetime->getTimestamp()) * $yardstick;
$endTime = ($goaldatetime->getTimestamp() - $startdatetime->getTimestamp()) * $yardstick;
update_field('contest_middletime', $interimTime, $post_id);
update_field('contest_endtime', $endTime, $post_id);
wp_update_post(array( 'ID' => $post_id, 'post_title' => $skipper ));
}
add_action('acf/save_post', 'save_contest_post', 20);
Because the time that you’re using is based on a calculation and is not an exact “time” I don’t think that you can use this field to order your posts. To be honest, looking at your code I don’t even know what this value would be… a time difference multiplied by some other value. More that likely the posts are being sorted in the order of the field.
Yes its correct, its a time difference multyplied by a number. This is something like a handycap in golf so that different boat can start in the same competition. Why is this not working? I get the numbers into the field in wordpress, i can see them in the backend and the output fields. It should just order by these number which is a nummeric afc field.
I also tried with:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'contest',
'meta_key' => 'contest_endtime',
'orderby' =>'meta_value',
'order' => 'ASC'
));
Yes, in order to sort y a meta cay you need to use that field for meta_key. Should have seen this but I missed it. In order to sort by your time field but select posts based on
'meta_key' => 'contest_route',
'meta_value' => 'route_small',
you’re going to need to use a meta_query rather than the above for contest_route. meta_query is an array. Look at the meta_query section of https://codex.wordpress.org/Class_Reference/WP_Query
The topic ‘ACF posts orderby not working correct’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.