Byeongin Yoon
Byeongin Yoon

Reputation: 4077

WordPress How can I get post_id from thumbnail_id?

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

Answers (2)

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

Azad Chouhan
Azad Chouhan

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

Related Questions