WordPress.org

Ready to get started?Download WordPress

Forums

get post custom keys -and- order by value (5 posts)

  1. antistandard
    Member
    Posted 3 years ago #

    Hi everyone,

    I have succeeded in getting get_post_custom_keys to work in retrieving a list of all the custom keys(names) associated to a post, but I would like to re-arrange the names in output.

    WordPress custom fields arranges the names of these fields in alphabetical order and I can't find a way to make it re-order the output.

    For example:
    Custom Fields:
    Name- Bee Value- 3
    Name- Crocodile Value- 2
    Name- Dragon Value- 6

    If this was outputted, using get_post_custom_names, the result would be:
    Bee
    Crocodile
    Dragon

    But I would like to order the result by value DESC

    so my output would be:
    Dragon
    Bee
    Crocodile

    Here is my code:

    <?php
      $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        echo $key . " => " . $value . "<br />";
      }
    ?>

    Does anyone know a simple way to alter the code so I can achieve my goal. Thank you for any help

    James

  2. vtxyzzy
    Member
    Posted 3 years ago #

    I think using rsort will do what you want:

    <?php
      $custom_field_keys = get_post_custom_keys();
      rsort($custom_field_keys);
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        echo $key . " => " . $value . "<br />";
      }
    ?>
  3. antistandard
    Member
    Posted 3 years ago #

    Hi vtxyzzy for your help.

    I was lookiing and didn't find any documentation on 'rsort' in the wordpress codex.

    are there any parameters i can use with it to sort alphabetically, or by value ?

    It would be perfect if I can sort the keys by value like in my example where the highest number (in the example 6 dragons) is first in the sorted output, then 3 bees and lastly 2 crocodiles.

    hope that makes sense.

    What I am trying to acheive is to sort the keys by using a number in the value field. So my client can put the highest number in the value for the key they want to appear first and so on..

    Thanks for your help

  4. vtxyzzy
    Member
    Posted 3 years ago #

    rsort reverse sorts an array by values, but does not maintain the keys.

    Replace the rsort with this:

    natsort($custom_field_keys);
    $custom_field_keys = array_reverse($custom_field_keys,true);
  5. antistandard
    Member
    Posted 3 years ago #

    Thanks again vtxyzzy,

    I understand what you mean now.

    If I was to then reverse sort the value instead of the keys, would i use:

    natsort($custom_field_values);
    $custom_field_values = array_reverse($custom_field_values,true);

    thank you for your time and help
    -james

Topic Closed

This topic has been closed to new replies.

About this Topic