0

I have a function in my WP which get the data from database,

Here is the function

function thebook_get_provider_types() {
    global $wpdb;
    static $provider_types;

    if (! isset($provider_types)) {
        $row = $wpdb->get_row(sprintf('SELECT * FROM wp_postmeta WHERE meta_id = %d', CF_PROVIDER_TYPES));
        $provider_types = unserialize($row->meta_value);
    }

    return $provider_types;
}

Is there any way to get the data by alphabetical sort? I wasn't able to find the related documentation in CODEX, Any help would be greatly appreciated.

  • 1
    An simple ORDER BY clause will order alphabetically. How is this WordPress related? – s_ha_dum Jun 10 '14 at 16:12
  • @s_ha_dum codex.wordpress.org/Class_Reference/wpdb It's not? – user3067592 Jun 10 '14 at 16:14
  • 1
    $wpdb is a thin wrapper for running SQL. Everything of significance in your code is SQL. You can run that code as easily without $wpdb as with it, and the answer is pure SQL syntax. Even in the context of WordPress, pure SQL is likely to be off-topic. – s_ha_dum Jun 10 '14 at 16:30
  • @s_ha_dum Thanks man, i didn't know that, thanks for the info – user3067592 Jun 10 '14 at 16:36
1

It isn't really WordPress-related indeed, but then again, OP doesn't seem to know that, so here's your answer :-). However, next time: this isn't WordPress-related, please post PHP-questions in the corresponding SE.

Sorting a PHP array can be achieved by using the sort function, which sorts an array alphabetically by its values.

I'm assuming you have a one-dimensional array. If not, you should really edit your question with more information.

function thebook_get_provider_types() {
    global $wpdb;
    static $provider_types;

    if (! isset($provider_types)) {
        $row = $wpdb->get_row(sprintf('SELECT * FROM wp_postmeta WHERE meta_id = %d', CF_PROVIDER_TYPES));
        $provider_types = unserialize($row->meta_value);
    }

    // Sort the array alphabetically
    sort( $provider_types );

    return $provider_types;
}
  • Hey, Thanks for the info, i didn't know that. Also it still doesn't sort my data, could you help me with that? – user3067592 Jun 10 '14 at 16:36
  • I've edited my answer, this should work (assuming that your array is one-dimensional). – engelen Jun 10 '14 at 16:40
  • Really appreciate it, actually somebody wrote this function, it's over my head to give you more info, but your code doesn't work. I'll update here once i get more info. – user3067592 Jun 10 '14 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.