I have a post type profiles, which has a repeater field named sd_tour_schedule.
Each schedule can have multiple dates and has 3 sub fields (city, from, until).
The output shows all posts, with the first upcoming date shown, but (as far as I can debug it) the posts get sorted by the ‘first’ post date in the repeater. So I want to sort the repeater after saving.
If I run this code, the values do get sorted (which I can see in the back-end) and on the single view, but on a page which queries this repeater value it’s not shown anymore. I think I might not store the subfield correctly.
Any insight to this ?
function sd_sort_tour_dates( $post_id ) {
if ( isset( $_POST[ 'acf' ][ 'field_5a0de683a21cf' ] ) ) {
$tour_dates = get_field( 'sd_tour_schedule', $post_id );
foreach ( $tour_dates as $key => $row ) {
$column_id[ $key ] = $row[ 'from' ];
}
array_multisort( $column_id, SORT_NUMERIC, $tour_dates );
$key = 0;
foreach ( $tour_dates as $key => $tour_info ) {
$key++;
if ( is_array( $tour_info ) && count( $tour_info ) > 0 ) {
foreach( $tour_info as $column => $value ) {
update_sub_field( array('sd_tour_schedule', $key, $column ), $value );
}
}
}
}
}
add_action( 'save_post', 'sd_sort_tour_dates' );
This is my meta query.
$query->set( 'meta_query', array(
'tour_clause' => array(
'key' => 'sd_tour_schedule_%_until',
'value' => date( 'Ymd' ),
'compare' => '>'
),
'move_up_clause' => array(
'key' => 'sd_move_item_up_timestamp',
'compare' => 'EXISTS'
),
));
$query->set( 'orderby', array( 'tour_clause' => 'ASC', 'move_up_clause' => 'DESC' ) );
And the filter which is called.
function sd_tour_city_filter( $where ) {
global $wpdb;
$where = str_replace(
"meta_key = 'sd_tour_schedule_%",
"meta_key LIKE 'sd_tour_schedule_%",
$wpdb->remove_placeholder_escape( $where )
);
return $where;
}
It returns the output but sorts by the row numbers/index. Hence why the order needs to be correct in the back-end and that’s why the order has to be ‘re-stored’ after saving a post. I disabled the ordering of repeaters on the front-end so users can’t change this. I don’t want them to think about this.
After some testing, I think this will do the trick.
if ( isset( $_POST[ 'acf' ][ 'field_5a0de683a21cf' ] ) ) {
$tour_dates = get_field( 'sd_tour_schedule', $post_id );
if ( $tour_dates ) {
foreach ( $tour_dates as $key => $row ) {
$column_id[ $key ] = $row[ 'from' ];
}
array_multisort( $column_id, SORT_NUMERIC, $tour_dates );
$key = 0;
foreach ( $tour_dates as $key => $tour_info ) {
if ( is_array( $tour_info ) && count( $tour_info ) > 0 ) {
foreach( $tour_info as $column => $value ) {
if ( 'city' != $column ) {
$value = date( 'Ymd', (int) $value );
}
update_post_meta( $post_id, 'sd_tour_schedule_' . $key . '_' . $column, $value );
}
}
$key++;
}
}
}
You must be logged in to reply to this topic.
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!
ACF PRO’s Flexible Content field allows you to create smaller pieces of UI and compose them into whole flexible patterns. In our latest article, we show how to use it to create swappable site sections and integrate it all in a theme.https://t.co/ZRocH8oJSp
— Advanced Custom Fields (@wp_acf) January 24, 2023
© 2023 Advanced Custom Fields.
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.