• Hey guys. I’m using get_post_meta to get an array of all values from a certain set of custom fields. I can display them, but they are in alphabetical order and I NEED them in the order I submitted on the actual page with the custom fields. For example, I enter 3 different values for the same custom field:

    [key] Expansion
    [value] The Burning Crusade

    [key] Expansion
    [value] Wrath of the Lich King

    [key] Expansion
    [value] Cataclysm

    I enter those in that order, and that’s the order I need them to be displayed. I looked at the codex but it doesn’t give much information on get_post_meta.

    If there’s no way to display custom fields in the order they are entered, does anyone else have an idea of how I could achieve the same result?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter ciaravino

    (@ciaravino)

    Here’s a revised post with better information. It doesn’t let me edit the original post anymore ??

    Hey guys. I’m using get_post_meta to get an array of all values from a certain set of custom fields. Currently, I guess by default, the get_post_meta array returns all the values starting with the most recent submitted value instead of the first value. For example, I enter 3 different values for the same custom field in this order:

    [key] Expansion
    [value] The Burning Crusade

    [key] Expansion
    [value] Wrath of the Lich King

    [key] Expansion
    [value] Cataclysm

    But the array returns them in reverse, so Cataclysm gets displayed first, followed by Wrath of the Lich King; which is opposite of what I need. I wouldn’t mind entering all the custom field info in reverse order (so it would reverse the reverse and come out correct) but I wouldn’t be able to add any more values that I didn’t add the first time. Like, if an expansion came out in 5 months and I wanted to go add that to the custom fields, I would have to re-enter all of them to be able to get them in the correct order.

    If there’s no way to display custom fields in the order they are entered, does anyone else have an idea of how I could achieve the same result?

    Thread Starter ciaravino

    (@ciaravino)

    Thanks for those posts but it looks like you need to edit query.php for those. I was hoping for some extra parameter that wasn’t in the codex ??

    What about a normal PHP sort function or something? Do you happen to know one that would work? I’m still like a beginner ??

    Thread Starter ciaravino

    (@ciaravino)

    I have an update:

    Okay. I have two pages that use a key more than once. One page, I added like 12 values for the key and they all came out in the correct order. The other page, however, I add 1 value for 1 key and it becomes [0], like it’s supposed to. But, when I add a second value for the key, the first value for the key moves down to [1] and the 2nd value goes to [0]. Then, I add a 3rd value for the key and it makes the first value go to spot [2], the 2nd value stay at spot [0], and then takes the place of spot [1]. I have NO idea why it is jumbling the array spots… I checked the database and the meta_ids are in the correct order. I’ve tried adding one value for the key then updating the page and adding another so each one gets added each update, but that didn’t work either.

    I’m about to look into sorting the values depending on meta_ids… I just don’t get why it works on one page when it doesn’t on the other when they use the same template and the values are for the same key. I actually think I’ll try deleting the page and redoing it.

    EDIT: Oddly, after deleting the post completely and remaking and re-adding the values, they display in the correct order.

    I had this same issue and just did a direct query to the table to get it in order:

    // Get Custom values with key "Expansion"
       global $wpdb;
       $sql = "SELECT m.meta_value FROM wp_postmeta m where m.meta_key = 'Expansion' and m.post_id = '".$post->ID."' order by m.meta_id";
       $results = $wpdb->get_results( $sql );
       foreach( $results as $result )
       {
          $expansionVal = $result->meta_value;
          // Use value here
       }

    I had a similar problem with multiple custom fields with the same keys and solved it by patching /wp-includes/meta.php around line 294:

    $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) order by meta_id", $meta_type), ARRAY_A );

    Just added ‘order by meta_id’ to ensure they get back in the order I entered them. Since meta_id is autoincrement in the DB this should do it, and I think it should be added to the core permanently since the current order is random (and useless).

    This makes updating a problem since you’ll have to restore this hack every time ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is it possible to display the get_post_meta array in order?’ is closed to new replies.