mbrc
mbrc

Reputation: 3953

How to get title, name and description for an image that I upload in WordPress?

I wrote this Query:

$album_id = get_the_id();
$photos = $wpdb->get_results(
    "select * from wp_postmeta where post_id = '"
    . $album_id
    . "' AND meta_key = 'gallery_ph' order by meta_id desc"
);

Now, I want to get all thumbnails with title, name and description for this images.

I found in the DB:

meta_id | post_id | meta_key   | meta_value
577     | 346     | gallery_ph | http://url to the image

and the thumbnail

meta_id | post_id | meta_key                 | meta_value
569     | 348     | _wp_attachment_image_alt | Jury Datiop

How are this two datas connected? I know that Jury Datiop has the url http://xxx because I can check in wp-admin. But how to write the select query? How to connect image and get title, name, description?

Upvotes: 1

Views: 1279

Answers (1)

brasofilo
brasofilo

Reputation: 26055

You don't need to query the database directly.

WordPress has a whole lot of custom functions for you to retrieve the data that you need.

Check the:

WordPress Codex : Function Reference

Specially:


Translation of your database query to a WordPress function:

code not tested, use the Debug to inspect the results of each operation

$album_id = get_the_id();

$photos = get_children( array(
    'post_parent'    => $album_id, 
    'post_type'      => 'attachment', 
    'meta_key'       => 'gallery_ph', 
    'post_mime_type' => 'image', 
    'order'          => 'DESC', 
    'orderby'        => 'meta_id') 
);

// DEBUG
// echo '<pre>' . print_r( $meta, true ) . '</pre>';

if( $photos )
{
    foreach( $photos as $img )
    {
        // DEBUG
        // echo '<pre>' . print_r( $img, true ) . '</pre>';

        $meta = wp_get_attachment_metadata( $img->ID );
        
        // DEBUG
        // echo '<pre>' . print_r( $meta, true ) . '</pre>';
    }
}   

Upvotes: 1

Related Questions