• Hey, I’ve just spent the last hour searching for a way to do something that SEEMS like it should be simple, but maybe not…

    How can I find out which post(s) include a specific image? I’m looking through the database and I see the NGG pictures, albums, and galleries, but the only place I see the actual post/image information is in the post_content field in the posts table.

    What I’d like to be able to do is, for example, given picture pid=5, find out that it was included in posts IDs 57 and 123. Or, on the flip side, find out which images haven’t been included in any posts.

    I’m not that familiar with all of the workings of WP (although I’m happy to learn), but I’m very comfortable with PHP, so I can write something myself if necessary. I’m just hoping I won’t have to start by parsing the actual text of all my posts…

    Ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m assuming WP will do the most sensible thing and ‘attach’ the images to the posts (and not vice versa). Therefore, given an image, I would think that you have to go through all posts…

    But if you’re comfortable with PHP, parsing the posts shouldn’t be a problem at all. This probably isn’t something you want to do on every pageload though…

    Thread Starter swd_cb

    (@swd_cb)

    What I’m thinking of doing is creating a database table to associate NGG images with posts, and (re)populate it every time a post is saved. Then I can just query that table for whatever I might need.

    That sounds sensible. It’s surprising though that there isn’t a solution, yet. Maybe you should search extensively before starting from scratch.

    Thread Starter swd_cb

    (@swd_cb)

    I have searched extensively, until my eyes were about to fall out of my head. Posted here hoping I missed something.

    I started out using WP using NGG exclusively, so I’ve actually not used the default WP one. It’s my understanding, though, that it does somehow track image attachments the way I need them to be tracked, or, at least, close enough. (Going to test that out this weekend.) I wonder if that’s a planned upgrade to NGG at some point?

    (When I do get it working, I’ll be happy to share my code with whoever might know a bit more about making WP plug-ins. I’ve done a few other smaller useful (for me) upgrades to NGG as well. I’m a tinkerer. I can’t help it. ??

    Thread Starter swd_cb

    (@swd_cb)

    Here’s something I just threw together that actually seems to work:

    $content = get_post($post_id)->post_content;
    $pattern = '/\[singlepic.*?id=([0-9]+).*?\]/';
    preg_match_all($pattern, $content, $matches);
    $post_images = 0;
    if ($matches[1]) $post_images = join(',', $matches[1]);

    That gives me (in this particular case) a comma-separated list of image IDs that I’m using in a SQL query.

    I’m really too worn-out today to be trying to code, but I had some time to kill. So if anyone wants to point and laugh, at least post the fix, please. ?? Especially on the regex part, as I think I did that weirdly. But it does appear to be working correctly so far.

    By the way, I have a number of planned uses for this (including my previously-mentioned putting this all in a database table idea), but right now, I’m actually using it in media-upload.php to allow me to use a checkbox to hide images I’ve already included in the post I’m editing. Quite useful when you have 40 photos in a gallery that you want to put in a post (my blog is really photo-heavy) and you only have 39 in the post and can’t for the life of you figure out which one you missed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: NextGEN Gallery] Listing images in posts’ is closed to new replies.