Skip to main content
Tweeted twitter.com/#!/StackWordPress/status/329324266090799104
Changed code to reflect advice in first answer.
Source Link
Eckstein
  • 1.1k
  • 4
  • 27
  • 52

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', truefalse)) {
            $gallery = get_post_meta($post_id, 'create_gallery', truefalse);//get the full gallery
            $firstrow$attachment_id = $gallery[0];$gallery[0]['upload_image']; //Only workGet withimage thefrom first row in the database
            $attachment_id = $firstrow['upload_image']; //Set the first image to the feature image ID
        }

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?

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:

//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', true)) {
            $gallery = get_post_meta($post_id, 'create_gallery', true);//get the full gallery
            $firstrow = $gallery[0]; //Only work with the first row in the database
            $attachment_id = $firstrow['upload_image']; //Set the first image to the feature image ID
        }

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?

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?

Source Link
Eckstein
  • 1.1k
  • 4
  • 27
  • 52

Setting featured image with function, 1 part not working

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:

//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', true)) {
            $gallery = get_post_meta($post_id, 'create_gallery', true);//get the full gallery
            $firstrow = $gallery[0]; //Only work with the first row in the database
            $attachment_id = $firstrow['upload_image']; //Set the first image to the feature image ID
        }

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?