[resolved] Attachment's corresponding post's ID --> PHP Variable? (3 posts)

  1. theiconoclast31
    Posted 8 years ago #

    I'm creating a plugin that resizes uploaded images and autogenerates thumbs in a designated uploads/iconothumbs folder.

    Short version: I have the attachment info - I'd like to get the corresponding post's id, and store it in a variable.

    Long version: I've done all the resizing. I'd like to store the new, resized thumbnails into files. However, I don't know how to name the files.

    The filename should be a) unique and b) echo-able from within the loop, with the post info, but without the attachment info.

    I'm working from the wp_handle_upload filter hook. I understand that each attachment has its own ID, but I need to somehow relate the thumbnails to their corresponding post. In the end, I want to have some kind of <?php iconothumb1(); ?> tag that automatically generates the thumbnail for a given post from the loop.

    This is my first PHP adventure, and my first WordPress plugin.

  2. theiconoclast31
    Posted 8 years ago #

    Okay, I've made some progress, and I can get the GUID of the attachment. With the GUID, can I get the post_parent (by querying the database)? I'm currently trying:

    $thisPostID = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE guid='$thisGUID'");

    But I keep getting Fatal error: Call to a member function on a non-object.

  3. craigtrader
    Posted 8 years ago #

    You need to declare $wpdb as a global in your function:

    global $wpdb;

Topic Closed

This topic has been closed to new replies.

About this Topic