0

Here's the deal: I was migrating the content (lots of posts) from an old site to a new one. The posts have multiple images attached to it (which I've noticed when I was importing the thumbnail images via WP All Import) and it is being nicely displayed in single.php template like this:

Main image

<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'full'); ?>
+ HTML markup

Little thumbnails beneath main image (if there are more than one images)

$images = sql_post_images( $ID = get_the_ID(), get_post_thumbnail_id( $ID ) );
foreach($images as $image) {
  HTML markup
}

sql_post_images() function

function sql_post_images( $post_id, $exclude_id = 0 ) {

    global $wpdb;

    $data = $wpdb->get_results(
        $wpdb->prepare(
        "   
            SELECT      DISTINCT wposts.ID, 
                        wposts.post_title,
                        wposts.post_excerpt,
                        wposts.post_content, 
                        wpostmeta.meta_value
            FROM        $wpdb->posts wposts
            LEFT JOIN   $wpdb->postmeta wpostmeta
                        ON wpostmeta.post_id = wposts.ID
                        AND wpostmeta.meta_key = %s
            WHERE       wposts.post_parent = %d
            AND         wposts.post_type = 'attachment'
            AND         (wposts.post_mime_type = 'image/jpeg' OR wposts.post_mime_type = 'image/png' OR wposts.post_mime_type = 'image/gif')
            AND         wposts.ID != %d
            ORDER BY    wposts.menu_order ASC

        ", '_wp_attachment_metadata', $post_id, $exclude_id), ARRAY_A);

    return( $data );
}

This works well and I would like to keep it like that in the future as well - the problem is I cannot find a solution to allow upload of multiple images to a post. I found some plugins, but their approach is clearly different (it's either just a second thumbnail image, or something similar). What could actually work?

EDIT: Uploading images as a classic WP attachments doesn't seem to do the trick.

1 Answer 1

1

Have you considered using the ACF plugin? ACF has a very convenient field type - gallery. I think it would match what you want a 100% without the need to poke around the native/custom DB queries. Check it out.

5
  • I know about the gallery field, it's more about 900+ posts already created and being displayed this way. The perfect solution for me would be to find a way to upload the images to fit this scenario, rather than making changes to my templates. Commented Oct 29, 2018 at 13:46
  • 1
    It usually works the other way around... you find a solution, and then adjust your templates to implement it. :)
    – djboris
    Commented Oct 29, 2018 at 13:51
  • I absolutely agree, but in this case I have a working template and a solution that worked on my client's site for quite some time. Adapting the site to another solution (e.g. ACF gallery) would mean adjusting the .php templates, adjusting CSS respectively and migrating those images from "attachments" to those galleries. This sounds like a lot of more work than just finding a piece of code or a plugin, that allows uploading multiple post thumbnails. In the end, it worked on the older site. But I get your point. Commented Oct 29, 2018 at 13:54
  • It actually makes sense in your case, I agree with you. But the way you have done it, you heavily depend on the wp_posts table, while WP uses it only to store the image ID. There is some linking done directly in the wp_posts table using post_parent column, but this is not used for retrieving the thumbnail id from the database, that info is stored in the wp_postmeta table. That way it is possible to reuse the same attachment in multiple posts, without duplicating the entry in the wp_posts table, but post_parent stays tied to the first post it got uploaded with.
    – djboris
    Commented Oct 29, 2018 at 14:28
  • What I meant to say, there is really a tiny chance you'll find a working solution for your case. But anyway, try searching for wp add multiple featured images.
    – djboris
    Commented Oct 29, 2018 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.