0

Is there a smart way to order an array by the acf radio button values? I have a top-page page-template, and a couple of child pages with custom fields.

In the top-page i want to display some values from the child pages: page title, and some values from the child pages acf fields And finally i want to order the array by the acf radio button values.

The radio button values is:

one : A 
two : B
three : C
four : D
five : E

The field is mandatory so i don't have to test to see if there is a value.

        <section id="info-box">

            <?php
            if(function_exists( 'get_field') ){

            $query = new WP_Query( 'pagename=top-page' );
            $services_id = $query->queried_object->ID;

            wp_reset_postdata(); //* Restore original Post Data *//

                $args = array(
                'posts_per_page' => -1,
                'post_type' => 'page',
                'post_parent' => $services_id,
                'orderby' => '', // radio button value //
                'order' => 'ASC'
                );

                $services_query = new WP_Query( $args );

                    // The Loop
                    if ( $services_query->have_posts() ) {

                        echo '<ul class="info-box-list">';
                            while ( $services_query->have_posts() ) {
                            $services_query->the_post();

                            echo '<li class="list-item">'; 

                                echo '<div class="list-item-box">';

                                echo '<a href="' . get_permalink() . '" figure class="link-box-image">';
                                the_field('image');
                                echo '</figure>';
                                echo '</a>';

                                echo get_the_title();
                                the_field( 'sub-title' );

                                echo '</div>';
                            echo '</li>';
                            }
                        echo '</ul>';
                    }

            /* Restore original Post Data */
            wp_reset_postdata();
            }
        ?>  
       </section>

Right now I'm kinda just dumping the fields onto the top-page until i can figure out if there is a smart way to order the list by the radio button value, the acf documentation is not that good on the radio buttons functionality.

nicklas bryntesson
  • 149
  • 1
  • 3
  • 12

1 Answers1

1

What you need is order by a custom field, but be careful because your radio button values' (one, two, three...) real order is:

  1. five
  2. four
  3. one
  4. three
  5. two

(That's it: alphabetical) So, if you want to order by numbers, you better set your radio button values as:

1 : A 
2 : B
3 : C
4 : D
5 : E

After this, you should set your WP_Query's $args array this way:

$args = array(
    'posts_per_page' => -1,
    'post_type'      => 'page',
    'post_parent'    => $services_id,
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'YOUR_RADIO_BTN_FIELD_NAME',
    'order'          => 'ASC'
);

This should do the trick :)

Jordi Nebot
  • 3,355
  • 3
  • 27
  • 56
  • This doesn't work for me as it's a more complicated query. Could you help me please? [here](http://stackoverflow.com/questions/42371746/is-it-possible-to-query-posts-in-the-same-order-as-assigned) – Antonios Tsimourtos Feb 28 '17 at 13:05