Why does $wpdb return strings for mysql integer values?

As the other gentlemen have mentioned in the comments, due to the libraries that source uses, strings are returned. If you need to convert to an integer for a comparison or function call, just use WordPress’s absint function .

$int_id = absint( $wp_posts[0]->ID );

Also, not sure if your DB call is just simply an example, but I suggest using get_posts, or better yet, a WP_Query if possible. This allows you to maintain all of the sanitization those functions have built into them, keeping the security of your site pretty solid. Also remember to use wp_reset_query after using the above to methods to ensure you are reverting back to the main loop.

Leave a Comment