WordPress.org

Ready to get started?Download WordPress

Forums

Retrieve post meta in the same order it was entered (2 posts)

  1. akmjenkins
    Member
    Posted 3 years ago #

    So I've been trying to get post meta out the same order that I put it in:

    email_label 'Me'
    email_label 'My Friend'
    email 'me@me.com'
    email 'myfriend@friend.com'

    Yet it doesn't consistently come out in the same order. The reason I want it to come out in the same order is so I can maintain index association so if I did this:

    $labels = get_post_meta($postID,'email_label');
    $emails = get_post_meta($postID,'email');
    
    foreach($labels as $key=>$label) {
      echo $label." ".$emails[$key];
    }

    The index association would hold up and everything would match. I also want to keep the fields email_label and email separate so I can query the meta (as opposed to serializing the label and email in an array and sticking it inside a single post meta key).

    I figured out it's quite easy to do this, we just need to add an order by clause to line 330 of wp-includes/meta.php like follows:

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

    (of course, I had to use $idcol to determine which key to order by depending on if the usermeta or postmeta table was being queried).

    I think retrieving meta in the same order it was entered is a valuable feature to add to WordPress, and such a simple clause would be very unlikely to break any legacy functions of WordPress seeing how there is currently no dependent order when the meta comes out.

  2. s_ha_dum
    Member
    Posted 3 years ago #

    I don't really consider the 'meta' information to be ordered but if you want to get it back in the order you put it in just ORDER BY meta_id. That is an 'auto increment' column. The first value in should always have a lower meta_id. If you make a mistake when you input the values you can't correct it though.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.