WordPress.org

Support

Support » Plugins and Hacks » Hacks » Order metadata when retrieving via get_metadata()

Order metadata when retrieving via get_metadata()

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there,

    Using post meta is definitely not the best solution for this. You cannot currently control the order in which it is returned…I am having a similar problem with a plugin I’m developing.

    For your client’s needs, consider my plugin: http://wordpress.org/extend/plugins/csv-to-sorttable/

    I’m working on a newer version that is more feature-rich, but this one should do the trick.

    Cheers,

    Shaun

    Thanks Shaun – That’s a great idea and I will keep track of your plugin. For this project I’m already on another path. I’ve made my plugin to take the csv from an import and automatically create a post from each line. Ordering by the meta_id worked great, BTW, but I had to do a $wpdb query. I’ll paste it here in case anyone else can use it in the future:

    global $wpdb;
    $this_post = get_the_ID();
    $sql = "SELECT * FROM wp_postmeta WHERE post_id = $this_post ORDER BY meta_id";
    $meta = $wpdb->get_results( $sql );
    
    foreach ( $meta as $row ) {
        echo $row->meta_key; // Do fun thing here
    }

    Glad you were able to get it working. I would urge you to reconsider — not because I’m trying to push my plugin, but because your current solution does a few undesirable things:

    1. Fills up the database with data that is already stored neatly in a CSV file.
    2. Runs an SQL query to display data, which slows things down.
    3. Makes your ‘posts’ unusable in the future, since it doesn’t appear you’re using a custom post type.

    Each of those things seem unnecessary. If it is the route you decide to go, however, consider creating a custom post type for table data and using WP_Query instead of $wpdb.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Order metadata when retrieving via get_metadata()’ is closed to new replies.