I'm trying to run a custom sql query against the WordPress wp_postsmeta table. However, I can't seem to get CONCAT_WS to work as it did when I ran the query in the MySQL command line.

Right now I'm just trying to get CONCAT_WS to work. This is the relevant code:

global $wpdb;
$query = "SELECT $wpdb->postmeta.post_id, CONCAT_WS(',', $wpdb->postmeta.meta_key, $wpdb->postmeta.meta_value) from $wpdb->postmeta;";
$result = $wpdb->get_results($query, OBJECT);

This is $query from the end of var_dump: [...] 'SELECT wp_postmeta.post_id, CONCAT_WS(',', wp_postmeta.meta_key, wp_postmeta.meta_value) from wp_postmeta' (length=105)

This is what is returned:

0 => 
    public 'post_id' => string '2' (length=1)
    public 'CONCAT_WS(',', wp_postmeta.meta_key, wp_postmeta.meta_value)' => string '_wp_page_template,default' (length=25)

Seems like it returns part of the query in the result.

Is CONCAT_WS not possible to use with WordPress?

1 Answer 1


'CONCAT_WS(',', wp_postmeta.meta_key, wp_postmeta.meta_value)' was the name of the column. Everything is working as it should.

To change the name of the column, add AS name after the CONCAT_WS statement, like this:

'CONCAT_WS(',', wp_postmeta.meta_key, wp_postmeta.meta_value)' AS name (I didn't test this specifically).

Now the new column will be called name.

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.