Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to set a post's featured image based on custom fields. There are different types of posts and therefore the featured image should be set depending on which custom fields have been filled out.

Here is what I currently have (edited):

//Set post thumbnail based on various conditions
        if (get_post_meta($post_id, 'featured_image', true)) {
            $attachment_id = get_post_meta($post_id, 'featured_image', true);
        } elseif (get_post_meta($post_id, 'upload_single_image', true)) {
            $attachment_id = get_post_meta($post_id, 'upload_single_image', true);
        } elseif (get_post_meta($post_id, 'create_gallery', false)) {
            $gallery = get_post_meta($post_id, 'create_gallery', false);//get the full gallery
            $attachment_id = $gallery[0]['upload_image']; //Get image from first row    
        }

The first two conditions are working, but the third condition (create gallery) isn't. I thought that getting the first row of the db table and then calling the name of the custom meta data would do it, but it's not working.

Anyone see what I might be doing wrong?

share|improve this question
    
Some more context: Each custom field outputs the ID of an image. I've double checked that this it the case. Also, 'create_gallery' retrieves two values, 'upload_image' and 'image_caption'. I am using the plugin Advanced Custom Fields to loop through this data to allow my users to create galleries of images/captions. I just want to the first image in the table to be the featured image. –  Eckstein Apr 30 '13 at 18:11

2 Answers 2

You have to set the $single paramter of get_post_meta to false (or don't set it) to get an array.

Here is a sped-up version:

    if ($attachment_id = get_post_meta($post_id, 'featured_image',      true)) :
elseif ($attachment_id = get_post_meta($post_id, 'upload_single_image', true)) :
elseif ($attachment_id = get_post_meta($post_id, 'create_gallery',     false)) :
    $attachment_id = $attachment_id[0]['upload_image'];
endif;

Note: I did not test this.


// EDIT
Here is the code with the ACF function:

    if ($attachment_id = get_post_meta($post_id, 'featured_image',      true)) :
elseif ($attachment_id = get_post_meta($post_id, 'upload_single_image', true)) :
elseif ($gallery = get_field('create_gallery', $post_id)) :
    $attachment_id = $gallery[0]['upload_image'];
endif;
share|improve this answer
1  
Thanks, that makes sense. I have updated the question with my current code based on your answer. But it's still not setting the first image as the featured image for some reason. –  Eckstein Apr 30 '13 at 18:26

Got it to work by using ACF's custom functions instead of wp's meta functions.

Final working result:

//Set post thumbnail based on various conditions
        if (get_post_meta($post_id, 'featured_image', true)) {
            $attachment_id = get_post_meta($post_id, 'featured_image', true);
        } elseif (get_post_meta($post_id, 'upload_single_image', true)) {
            $attachment_id = get_post_meta($post_id, 'upload_single_image', true);
        } elseif (get_field('create_gallery', $post_id)) {
            $gallery = get_field('create_gallery', $post_id);//get the full gallery
            $attachment_id = $gallery[0]['upload_image']; //Get image from first row    
        }
share|improve this answer
    
Hey, nice you figured the last case out by yourself, however, you really should use the if-elseif construct like I did, otherwise you'd call each of the get_* functions twice, which is unnecessary. See my updated answer on this. –  t f Apr 30 '13 at 19:34
1  
I see. I've never structured my conditionals like that, but I guess it makes sense to not have to call it twice. I'll go ahead and change it. Thanks for the advice! –  Eckstein Apr 30 '13 at 20:27
1  
Well, it's not the ordinary conditional structure, that's right. But in this very case - where you want to set the value of a variable by a function, under the circumstance that the function does return something (meaning: not null, false etc.) - then you can do it like illustrated in my answer. Your approach is definitley the intuitive one, and for most of the time anyone would do it like that. Here, it would mean each function is called twice, though. –  t f Apr 30 '13 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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