If you really want to make a query in Wordpress need to use the $wpdb
which already offers a complete interface for everything you need.
Your consultation could be done as follows:
global $wpdb;
$id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_type = 'photo' ORDER BY ID DESC LIMIT 1" );
Note that in the case used $wpdb->posts
, since the installation of each can not always have the prefix wp_
. In addition that if it was necessary to consult a table other than the standard Wordpress should use something like:
{$wpdb->prefix}nome_da_tabela
Another thing I changed in your consultation was that I did it just to get the ID
, since it makes no sense to consult multiple columns if you only need one.
Exactly for only needing a value I used the method $wpdb->get_var()
. This way you don’t have to worry about extracting the value from an object or an array.
But usually to make queries in Wordpress posts you can utilize the class WP_Query
or the function get_posts()
(creating an interface for the WP_Query
).