Retrieve a WordPress attachment from outside of WordPress

I want to be able to grab a photo that has been attached to a WordPress post and display it independently of my WordPress install.

My usage is that I display an excerpt from the most recent post on a page that exists outside the scope of my WordPress install (like, mysite.com/blog/ is where WordPress exists but on mysite.com/index.php I display the excerpt from the latest post) and now I want to be able to attach an image to the post (not necessarily the post author, rather, any arbitrarily attached image… perhaps not even displayed in the post… just attached to it).

Read More

I grab the excerpt of the latest post with my own class that connects to the wordpress database and runs my own SQL queries against it. This is pretty straight forward. Problem is, I don’t see how an attached image is associated with a post!

In looking into the WordPress code itself I see the following function:

function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false )  {
        if ( !empty( $deprecated ) )
            _deprecated_argument( __FUNCTION__, '2.5' );

        if ( $fullsize )
            echo wp_get_attachment_link($id, 'full', $permalink);
        else
            echo wp_get_attachment_link($id, 'thumbnail', $permalink);
     }

This looks like it might grab an attached image but I still don’t see how (nor do I know how to use this function from outside of WordPress).

How would you grab an image, attached to a WordPress post, for display outside of a WordPress install?

Extra… Here is how I grab the post text in the first place:

SELECT * FROM wp_posts p
            INNER JOIN wp_term_relationships rs ON p.ID = rs.object_id
            INNER JOIN wp_term_taxonomy tt ON tt.term_id = rs.term_taxonomy_id
            WHERE rs.term_taxonomy_id IN ($termId) AND p.post_type = 'post'
            AND p.post_status='publish'

But since I see nothing in the database about an attached photo I don’t know how to go about grabbing it. Ideas?

More extra… Here is how I would grab such a photo from within the WordPress “loop”:

$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}

But of course I can’t just call that from completely outside of WordPress.

Related posts

Leave a Reply

2 comments

  1. Attachments are listed in the wp_posts database. They are associated like: post_type = 'attachment', post_parent = post_id. But I think you knew that.

    Edit

    Also, you can have access to the wordpress class and functions by including wp-blog-header.php from wordpress’ main directory. So now that you can use native wordpress functions, check out Image Attachment Recipies

  2. Have you tried using Custom Fields? You could create a new custom field named “attached_image” or whatever you want, with the url of the image you want to attach. Then you just retrieve that information from the database. I think they are saved in the table “yourprefix_postmeta” which has the fields ‘post_id’ (your post) and ‘meta_key’ (“attached_image” in my example).