Skip to content

Instantly share code, notes, and snippets.

@bmoredrew
Last active May 7, 2022 08:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoredrew/62411542bc045da7aca41c87d99f6edd to your computer and use it in GitHub Desktop.
Save bmoredrew/62411542bc045da7aca41c87d99f6edd to your computer and use it in GitHub Desktop.
Sort Posts by ACF Repeater Field Using Multidimensional Array
<?php
// Create an empty array for storage
$post_data = array();
// Set arguments for your query
$args = array(
'post_type' => 'trips',
'posts_per_page' => 999
);
// Do our query with any arguments from above
$query = new WP_Query( $args );
if ( $query->have_posts() )
{
while ( $query->have_posts() )
{
$query->the_post();
// Setup our repeater
if( have_rows('trip_info') ):
while ( have_rows('trip_info') ) : the_row();
// Optional - Only get posts that match a true false within our repeater
if ( get_sub_field('featured_trip') )
{
// Build our array with the data we need using key => value
$post_data[] = array(
'title' => get_the_title(),
'date' => get_sub_field('trip_date')
);
}
endwhile;
endif;
}
}
// Custom function to use in our sort to list the items by date
function subval_sort( $a, $b )
{
if ( $a['date'] == $b['date'] )
return 0;
return $a['date'] < $b['date'] ? -1 : 1;
}
// Sort our multidimensional array by sub array value
usort( $post_data, 'subval_sort' );
// We can now work our data normally through easy to access methods
foreach ( $post_data as $post )
{
echo $post['title'] . '&nbsp; ' ;
echo $post['date'];
echo '<br/>';
}
@ntenhoedt
Copy link

good tip! I've noticed the code does not account for years but only looks at day and month am I correct?

@ntenhoedt
Copy link

Just found out that I had set the return format wrong, I now went for the Ymd value (e.g. 20210727) and got it to work with years too. See below how I displayed it different on the site.

I made the calendar as such that it only displays upcoming events.

`$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();

    if ( have_rows( 'events' ) ):
    while ( have_rows( 'events' ) ) : the_row();
   
    $today = date("Ymd");
    $date = get_sub_field( 'date' );

if ( $date >= $today ):

        // Build our array with the data we need using key => value
        $post_data[] = array(
            'url' => get_the_permalink(),
            'title' => get_the_title(),
            'date'  => get_sub_field( 'date' ),
            'location'  => get_sub_field( 'location' ),
            
        );
  endif;
    endwhile;
    endif;
}

} `

  • For anyone interested I then displayed the date in European format.

`<?php $format_in = 'Ymd';
// the format your value is saved in ( set in the field options )
$format_out = 'd M ‘y';
// the format you want to end up with

$date = DateTime::createFromFormat( $format_in, $post['date'] );

echo $date->format( $format_out ) . '&nbsp; ' ;
?>

`

@ntenhoedt
Copy link

The only additional thing I would be interested in is having a function that can limit the results. If I want to have a more compact calendar showing only the 10 upcoming events on the homepage for example.

@ntenhoedt
Copy link

ntenhoedt commented Jul 29, 2021

Found a solution! By adding a counter I was able to limit the rows.

If anyone is interested, add the counter after the while statement.

<?php $i = 0; ?>

And then add this with your own value after the first row in the for each statement:

<?php $i++; if( $i > 2 ) { break; } ?>

@ladygeekgeek
Copy link

Thanks for this I'm using this to sort events (with multiple dates as acf repeater field) by date. Is there any easy way to add the shared event date as a header with all posts sharing same date beneath it? In essence event listings by day?

@ntenhoedt
Copy link

ntenhoedt commented May 7, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment