2

I am trying to get favorite post ids from my table with get_col, but the results return ids as string instead of int. How can i get them directly as int ?

$sql = "SELECT post_id FROM {$table} $where
            GROUP BY post_id
            ORDER BY post_type
            LIMIT $offset, $count";
$wpdb->get_col( $sql );

Image: http://s16.postimg.org/b2khhvlxx/wpdb_Untitled_1.jpg

2
  • it returns a string because that's the way this works... it should return a string but with the value you selected. you can just change it to an int: $number = (int)$wpdb->get_col( $sql ); Commented Feb 4, 2014 at 7:08
  • Because wordpress' WPDB doesn't work with any model like structure, there's no way it knows the data should be INT, so it behaves as any MySQL query, and returns all data as strings. Commented Feb 4, 2014 at 9:56

1 Answer 1

1

Thank you for the comments. To answer my own question; there is no way to return the result as int, since this is the default behaviour of wp. So you can either use wp's native function: absint($result[$item]), or $result_int = array_map('intval',$result); I went with the latter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.