538 posts Themes and Graphics
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 100 and 499 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
+4 more
Bebel says

Hi,

yet again I ran into a problem I know to solve, but don’t like my solution and was hoping that somebody here can give me a hint.

Following thing: You know, when you upload an image with the wordpress uploader, you can get the “post url”, which, if the permalink setting is set to default, returns something like: http://foo.bar/?attachment_id=1337.

I need this attachment id to perform different actions with the attachment, before I display it. But I did not think of the different permalink settings. If they are changed, suddenly, the post URL displays something totally different, and doesn’t use the ID anymore. Instead, the file name is used.

So now I thought maybe there is an easy way to fetch the attachment by the filename. Although I already checked the functions, I did not find anything like “get_attachment_by_name()” or so. Is there anything I have overseen?

Otherwise I’d get all attachments of the post, get the attachment name with regex and check if it exists. But this seems like overhead to me..

Cheers Bebel

2429 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Microlancer Beta Tester
digitalimpact says

Hey,

I’m not sure how you’re planning on selecting&manipulating a certain attachment, but maybe this would help? (hope I understood what you’re after)

538 posts Themes and Graphics
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 100 and 499 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
+4 more
Bebel says

Actually nope :)

I have an custom field where people can upload an image. Because we want to use the wordpress thumbnailing feature, we need the attachment id in order to get the different image sizes.

in pseudo code:

$image = get_custom_value('foo-bar-image');

if(preg_match('attachment_id', $image, $bar)) 
    return $bar[1];
}else {
    // here its getting interesing. we want the image id, even if the post image url was not used or the permalink settings have changed
    return get_attachment_id_by_attachment_name($image);
}

I already have a working solution, which involves looping all the attached image files and check if the image url has a match. If so, return the attachment id.

I just wanted to know if there is anything more performant than looping through all the attachments :)

Just another question: Why do you have to access the database directly, if you could simply use:

$args = array(
      'post_type' => 'attachment',
      'numberposts' => 1,
      'post_status' => null,
      'post_parent' => $post_id
    );
    $attachments = get_posts($args);

I think its always better to use api functions for this. What, if in a future version, the database layout changes? :)

2429 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Microlancer Beta Tester
digitalimpact says
I just wanted to know if there is anything more performant than looping through all the attachments :)

Well, if you have more than 1 attachment, you would still need to somehow check them all, right?

I think its always better to use api functions for this. What, if in a future version, the database layout changes? :)

Hehe, true, sorry about that. Funny thing is I actually posted the exact solution you wrote some time ago on Forrst (with the help of another user, true) :D

538 posts Themes and Graphics
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 100 and 499 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
+4 more
Bebel says
I just wanted to know if there is anything more performant than looping through all the attachments :)
Well, if you have more than 1 attachment, you would still need to somehow check them all, right?

Hehe, sure thing, but if there would be a method from wordpress, that apparently does not exist, I’d not have to :P

Here’s what I have so far:

// .. more up here
    $args = array(
      'post_type' => 'attachment',
      'numberposts' => null,
      'post_status' => null,
      'post_parent' => $post_id
    );
    $attachments = get_posts($args);

    // check if it is an attachment and return the id
    if($attachments) {
      foreach ($attachments as $attachment) {
        $preg = pathinfo(basename($attachment->guid));
        if(preg_match('/'.$preg['filename'].'/', $url, $attachment_matches)) {
          return $attachment->ID;
        }
      }
      return false; // is not an attachment

    }
// .. more down here
I think its always better to use api functions for this. What, if in a future version, the database layout changes? :)
Hehe, true, sorry about that. Funny thing is I actually posted the exact solution you wrote some time ago on Forrst (with the help of another user, true) :D
Haha, well, at least you remembered now :P
by
by
by
by
by
by