Reputation: 4077
I'm developing wordpress plugin.
I need to find out post_id
from thumbnail_id
(not reverse !).
How can I do this?
Upvotes: 0
Views: 152
Reputation: 568
You can get result by this code
global $wpdb;
$_thumbnail_id = {thumbnail id};
$sql = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = $_thumbnail_id";
$result = $wpdb->get_results( $sql, ARRAY_A );
//access first returned post id
var_dump($result[0]['post_id']);
If you added same image for multiple posts there will be multiple returns.
Upvotes: 1
Reputation: 290
You can use get_the_ID() to get the post id. You can find this function in wp-includes/post-template.php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
Upvotes: 0