Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Order and categorise an ACF repeater field

Here's where I'm at. I need to create an 'index' (much like at the end of a book) on a site I'm building, based around an Advanced Custom Field repeater field.

Currently, you can add a 'programme' post (CPT), and add 'artist names' to that, which is currently set up as a repeater field with two text fields.

enter image description here

Now, I'm creating this 'index' page, which needs to list out all artist names, for all 'programme' posts, and organise them A—Z, filed under their own category. (See here for an example: http://minimalissimo.com/index/)

So, say across 4 posts, there's a total of 18 artist names, it would look something like:

A
Joe Allan
Frank Aztec

B
Jane Bank

C
Mike Crichton
Mandy Curtz

Using two methods, I can output all the names no problem. One way is to query the programme CPT, then simply do:

<?php if ( have_rows('artists') ) : ?>
    <?php while ( have_rows('artists') ) : the_row(); ?>
    <span><?php the_sub_field('first_name'); ?>&nbsp;<?php the_sub_field('last_name'); ?></span>
    <?php endwhile; ?>
<?php endif; ?>

Can you guys help at all? I realise it's a large question, but there may be something simple I am missing.

Thanks, R

User Gravatar

user-1043706

Posted Mar 12 2014 9:51 UTC

$50


  • Expired
  • php
    wordpress
  • 3098 Views

25 Replies


Hi R,

you can fetch the result order by last_name ASC

Then the below code will do the job for you:

<?php if ( have_rows('artists') ) : ?>
    <?php $previous = null; ?>
    <?php while ( have_rows('artists') ) : the_row(); ?>
    <?php 
        $firstLetter = substr(the_sub_field('last_name'), 0, 1);
        if($previous !== $firstLetter){
          echo "<br />".$firstLetter."<br />";
          $previous = $firstLetter;
        }
    ?>
    <span><?php the_sub_field('first_name'); ?> <?php the_sub_field('last_name'); ?>  </span>
   <?php endwhile; ?>
<?php endif; ?>

You can add span or other styling as you wish. If you don't have the data sorted by last name then you can generate a temporary associative array.

Let me know if you need more clarification.

Thanks, R

User Gravatar

rashidul

Posted Mar 12 2014 10:09 UTC

Hi @rashidul

Thank you for the help. Unfortunately, this doesn't seem to work? Using your code, I get the output below...

enter image description here

User Gravatar

user-1043706

Posted Mar 12 2014 10:40 UTC

Hi @R,

I'm sorry that It didn't work. I don't know much about wp. I gave the logic/algorithm behind the solution.

You may need to change the the_sub_field('last_name') to something else may be get_field('last_name'). You need to get the value of last_name.

$firstLetter = substr(the_sub_field('last_name'), 0, 1);
User Gravatar

rashidul

Posted Mar 12 2014 10:52 UTC

Hi again @rashidul

Nearly there! See below. I now need it to make sure all the names are grouped (rather than two Bs... it's B... and all the names beginning with B) I know you mentioned the list should be organised alphabetically by last name before this.

So

enter image description here

User Gravatar

user-1043706

Posted Mar 12 2014 10:55 UTC

use p tag instead of span for putting the data into new line

<span><?php the_sub_field('first_name'); ?> <?php the_sub_field('last_name'); ?>  </span>

to

<p><?php the_sub_field('first_name'); ?> <?php the_sub_field('last_name'); ?>  </p>
User Gravatar

rashidul

Posted Mar 12 2014 10:58 UTC

Can you show me the query/code where you retrieves the data from DB?

User Gravatar

rashidul

Posted Mar 12 2014 11:00 UTC

I need to then sort the repeater field (http://www.advancedcustomfields.com/resources/how-to/how-to-sorting-a-repeater-field/) can you factor this into the answer?

User Gravatar

user-1043706

Posted Mar 12 2014 11:00 UTC

<?php query_posts ( array ( 
    'post_type' => 'programme',
    'category_name' => 'archive',
    //'meta_key' => 'last_name', 
    //'orderby' => 'meta_value', 
    //'posts_per_page' => -1, 
    'order' => 'DESC' ) ); ?>

<div class="container_12">
    <div class="prefix_1 grid_10 suffix_1">
        <?php while ( have_posts() ) : the_post(); ?>



        <?php if ( have_rows('artists') ) : ?>
            <?php $previous = null; ?>

            <?php while ( have_rows('artists') ) : the_row(); ?>
            <?php $firstLetter = substr(get_sub_field('last_name'), 0, 1);
                if ( $previous !== $firstLetter ) {
                    echo "<br />".$firstLetter."<br />";
                    $previous = $firstLetter;
                }
            ?>
            <p><?php the_sub_field('first_name'); ?>&nbsp;<?php the_sub_field('last_name'); ?></p>
            <?php endwhile; ?>

        <?php endif; ?>




        <?php endwhile; ?>
    </div>
</div>
<div class="clear"></div>

<?php wp_reset_postdata(); ?>
User Gravatar

user-1043706

Posted Mar 12 2014 11:01 UTC

@R,

you wrote:

I know you mentioned the list should be organised alphabetically by last name before this.

Yes if you do, then the code will works exactly as you want

User Gravatar

rashidul

Posted Mar 12 2014 11:02 UTC

@rashidul and this was part of the job... I appreciate the help so far.

User Gravatar

user-1043706

Posted Mar 12 2014 11:03 UTC

Replace

//'orderby' => 'meta_value', 
//'posts_per_page' => -1, 
'order' => 'DESC' ) ); ?>

with

'orderby' => 'last_name', 
//'posts_per_page' => -1, 
'order' => 'ASC' ) ); ?>
User Gravatar

rashidul

Posted Mar 12 2014 11:04 UTC

Sorry message flow wasn't synchronized. I saw your post after I posted that msg.

User Gravatar

rashidul

Posted Mar 12 2014 11:05 UTC

@rashidul That's not valid in WP. In PHP can you order/sort a list?

User Gravatar

user-1043706

Posted Mar 12 2014 11:06 UTC

Oh, Ok. I'm writing the code. I'll get back to you shortly.

User Gravatar

rashidul

Posted Mar 12 2014 11:09 UTC

Will you try the below code. If the below code doesn't work, then my bad, I shouldn't try wp.

    <?php if ( have_rows('artists') ) : ?>
<?php $tmp = array(); ?>
<?php while ( have_rows('artists') ) : the_row(); ?>
<?php
    $tmp[] = get_row();
    $helper[] = get_field('last_name');
?>
<?php endwhile; ?>
<?php array_multisort( $helper, SORT_ASC, $tmp ); ?>
<?php foreach ( $tmp as $row ): ?>
<?php $previous = null; ?>
<?php $firstLetter = substr($row['last_name'], 0, 1);
                if ( $previous !== $firstLetter ) {
                    echo "<br />".$firstLetter."<br />";
                    $previous = $firstLetter;
                }
?>
<p><?php $row['first_name']; ?>&nbsp;<?php $row['last_name']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
User Gravatar

rashidul

Posted Mar 12 2014 11:36 UTC

updated the last post

User Gravatar

rashidul

Posted Mar 12 2014 11:38 UTC

I assumed get_row() returns an associative array

User Gravatar

rashidul

Posted Mar 12 2014 11:39 UTC

Hi @rashidul

Unfortunately this still doesn't work. get_row() isn't a function...

User Gravatar

user-1043706

Posted Mar 12 2014 13:24 UTC

you i resolved this problem?

User Gravatar

oirasor

Posted Mar 12 2014 14:32 UTC

@oirasor Close, but no.

User Gravatar

user-1043706

Posted Mar 12 2014 14:41 UTC

now i write my code for your solution

User Gravatar

oirasor

Posted Mar 12 2014 14:42 UTC

this my code

$artist = get_field('artists');

$lastNameArr = array();

foreach( $artist as $key => $row ) { $lastNameArr[ $key ] = $row['last_name']; }

array_multisort( $column_id, SORT_ASC, $artist );

foreach ( $artist as $row ): 

    $previous = null; 

    $firstLetter = substr($row['last_name'], 0, 1);

    if ( $previous !== $firstLetter ) {

        echo "<br />".$firstLetter."<br />";

        $previous = $firstLetter;
    }

echo '<p>'.$row['first_name'].' '.$row['last_name'].'</p>';

endforeach;

endif;

User Gravatar

oirasor

Posted Mar 12 2014 15:14 UTC

Thanks @oirasor but this doesn't work either. get_field('artists') isn't usable.

User Gravatar

user-1043706

Posted Mar 13 2014 9:52 UTC

You can use the below code, may be non-wp way and may not be optimal.

<?php if ( have_rows('artists') ) : ?>
<?php $tmp = array(); ?>
<?php while ( have_rows('artists') ) : the_row(); ?>
$firstLetter = substr(get_field('last_name'), 0, 1);
$tmp[$firstLetter][] = array('first_name' => the_sub_field('first_name'), 'last_name' => the_sub_field('last_name'));
<?php endwhile; ?>
<?php ksort($tmp); ?>
<?php foreach ( $tmp as $key=>$rows ): ?>
<?php 
    echo "<br />".$key."<br />";
    foreach ($rows as $row) {
        echo "<p> {$row['first_name']}   {$row['last_name']}</p>";
    }

?>
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
User Gravatar

rashidul

Posted Mar 13 2014 10:15 UTC

Hi,

I've a problem with ordering with repeater field, could please someone have a look, that would be very nice...? Thanks a lot

My Event Custom post type uses a repeater field containing Date and location. I’d like to order my event list by the first date field in the repeater of each event but i don’t k now what tp put in ‘meta_key’ ??? Thanks a lot

$wp_query = new WP_Query(array(
                    'post_type' => 'spectacle',
                    'posts_per_page' => $posts_per_page,
                    'meta_key'  => '????',
                    'orderby' => 'meta_value_num',
                    'order' => $order,
                    'paged' => $paged
                ));
User Gravatar

pipoulito

Posted Dec 4 2014 2:29 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions