Support

Account

Home Forums General Issues Sort by menu order with get_field_objects()

Solved

Sort by menu order with get_field_objects()

  • Hi,
    I want to be able to output the values I get from get_field_objects() in menu order.
    This is my code:

    $fields = get_field_objects();
    	if ( !empty($fields) ){
    		echo "<dl class='clearfix'>";
    		foreach ( $fields as $field ){
    			if (empty($field['value']))	continue;
    			echo "<dt>".$field['label']."</dt><dd>".$field['value']."    </dd>";
    			echo '<br class="clear">';
    		}
    		echo "</dl>";
    		}
    ?>

    But It doens’t output the values in the menu order.
    the menu order is

    but the output:

    “Hastalık Dayanımları (Rezistanslar):” should be after “Meyve hasat rengi:” as you can see in the order.

    Thanks you!

  • Hi @avishay28

    If you use var_dump($fields);, you will see that each field object has a “menu_order” value. You can use array_multisort() on this value to sort it like sorting a repeater field. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/.

    I hope this helps.

  • Hi,
    I var-dumped it and can’t see any “menu_order” there.
    The output:
    array(9) { ["Üretim Şekli:"]=> array(19) { ["key"]=> string(19) "field_53bd963ef0b34" ["label"]=> string(15) "Üretim Şekli:" ["name"]=> string(15) "Üretim Şekli:" ["_name"]=> string(15) "Üretim Şekli:" ["type"]=> string(4) "text" ["order_no"]=> int(0) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(25) "acf-field-Üretim Şekli:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(12) "Açık tarla" } ["Erkencilik:"]=> array(19) { ["key"]=> string(19) "field_53bd97c6f0b3b" ["label"]=> string(11) "Erkencilik:" ["name"]=> string(11) "Erkencilik:" ["_name"]=> string(11) "Erkencilik:" ["type"]=> string(4) "text" ["order_no"]=> int(1) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(21) "acf-field-Erkencilik:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(7) "Erkenci" } ["Bitki Yapısı:"]=> array(19) { ["key"]=> string(19) "field_53bd9650f0b35" ["label"]=> string(15) "Bitki Yapısı:" ["name"]=> string(15) "Bitki Yapısı:" ["_name"]=> string(15) "Bitki Yapısı:" ["type"]=> string(4) "text" ["order_no"]=> int(2) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(25) "acf-field-Bitki Yapısı:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(8) "Güçlü" } ["Meyve Uzunluğu:"]=> array(19) { ["key"]=> string(19) "field_53bd965cf0b36" ["label"]=> string(16) "Meyve Uzunluğu:" ["name"]=> string(16) "Meyve Uzunluğu:" ["_name"]=> string(16) "Meyve Uzunluğu:" ["type"]=> string(4) "text" ["order_no"]=> int(3) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(26) "acf-field-Meyve Uzunluğu:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(10) "18 - 22 cm" } ["Meyve Rengi:"]=> array(19) { ["key"]=> string(19) "field_53bd9667f0b37" ["label"]=> string(12) "Meyve Rengi:" ["name"]=> string(12) "Meyve Rengi:" ["_name"]=> string(12) "Meyve Rengi:" ["type"]=> string(4) "text" ["order_no"]=> int(4) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(22) "acf-field-Meyve Rengi:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(0) "" } ["Meyve Tipi:"]=> array(19) { ["key"]=> string(19) "field_53bd9671f0b38" ["label"]=> string(11) "Meyve Tipi:" ["name"]=> string(11) "Meyve Tipi:" ["_name"]=> string(11) "Meyve Tipi:" ["type"]=> string(4) "text" ["order_no"]=> int(5) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(21) "acf-field-Meyve Tipi:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(24) "Sivri Biber (Demre tipi)" } ["Hastalık Dayanımları (Rezistanslar):"]=> array(19) { ["key"]=> string(19) "field_53bd967cf0b39" ["label"]=> string(40) "Hastalık Dayanımları (Rezistanslar): " ["name"]=> string(39) "Hastalık Dayanımları (Rezistanslar):" ["_name"]=> string(39) "Hastalık Dayanımları (Rezistanslar):" ["type"]=> string(4) "text" ["order_no"]=> int(7) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(49) "acf-field-Hastalık Dayanımları (Rezistanslar):" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(2) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(8) "IR: TSWV" } [""]=> array(18) { ["key"]=> string(0) "" ["label"]=> string(0) "" ["name"]=> string(0) "" ["_name"]=> string(0) "" ["type"]=> string(4) "text" ["order_no"]=> int(1) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(10) "acf-field-" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["allorany"]=> string(3) "all" ["rules"]=> int(0) } ["default_value"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["value"]=> bool(false) } ["Meyve hasat rengi:"]=> array(19) { ["key"]=> string(19) "field_5672b9cead1fa" ["label"]=> string(18) "Meyve hasat rengi:" ["name"]=> string(18) "Meyve hasat rengi:" ["_name"]=> string(18) "Meyve hasat rengi:" ["type"]=> string(4) "text" ["order_no"]=> int(6) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(28) "acf-field-Meyve hasat rengi:" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(16937) ["value"]=> string(11) "Orta yeşil" } }

  • Hi @avishay28

    Do you use ACF version 4.x.x? If you do, the order value is set to the “order_no” key.

    I hope this helps.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Sort by menu order with get_field_objects()’ is closed to new replies.