Skip Navigation

[Resolved] Display custom fields I added to the (standard) Media Post Type in a Single Post

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by Luo Yang 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2049663

Tell us what you are trying to do?
I want to display in a Single Post the custom fields I added to the (standard) Media Post Type. This helps me display a field called Credit when I use an image in a Single Post. I use the standard Insert Media button from WP.

Is there any documentation that you are following?
I had a previous support interaction with Jamal
https://toolset.com/forums/topic/toolset-chat-support-ticket-by-arvidb-1620578849/

Is there a similar example that we can see?

What is the link to your site?
hidden link

=======================

I use the code provided by Jamal
Link: https://toolset.com/forums/topic/alt-text-output-for-images-from-custom-field/#post-1857657
and:
[types field='media-credit' item='[media-info field="wpcf-image-credit" info="id"]'][/types]

I also updated the functions.php code to include credit-media (used various different names)
case 'media-credit':
// return ("[wpv-post-body view_template='None' item='$attachment_id']");
return do_shortcode("[wpv-post-field name='_wpcf-media-credit' item='$attachment_id']");
break;

Now for featured images I get the right output using
[types field='media-credit' item='[wpv-post-featured-image output="id"]'][/types]

But when I use
[types field='media-credit' item='[media-info field="wpcf-image-credit" info="id"]'][/types]
I get no results.

When I just paste [media-info field="wpcf-media-credit" info="id"], I get: wp_toolset_post_guid_id

I even hardcoded the ID from one of my posts by pasting that id in the palce of the info="id" but that also didn't yield any results.

When I choose one of the other fields which is included in the functions code like title, or alt also don't work.

#2049941

Hello,

How do you setup the "media-credit" field?, if it is a repeating image field(multiple instances image field), you can try the solution of another thread:
https://toolset.com/forums/topic/how-to-retrieve-image-caption-alt-text-and-description/#post-227840

Since it is a custom codes problem, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page/post URL, where I can edit your custom PHP codes, I need a live website to test and debug, thanks

#2050937

Hi Luo,

I sent you a private message with the credentials to a live site.

The link you sent me:
https://toolset.com/forums/topic/how-to-retrieve-image-caption-alt-text-and-description/#post-227840

Is a couple of years older than the link sent by Supporter Jamal:
https://toolset.com/forums/topic/alt-text-output-for-images-from-custom-field/#post-1857657

The field is not repeating. Each image only has one credit value. I added the field through Custom Fields, and then I added them to the Post Type(s): Media.

These images can be added as a featured image, and I would like to show the content of the credit field. This has been resolved by Jamal. He then give me code that would also do that for images inserted in the body of a post, and that is not working.

I added the below code to functions.php:

function media_info_func( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'info' => '',
        ),
        $atts
    );
 
    global $post, $wpdb;
     
    $field = $atts['field'];
    $info = $atts['info'];
   
    $image_url = get_post_meta( $post->ID, $field );
     
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
    $attachment_id = $attachment[0]; 
     
    if ( $field && $info ) {
        switch($info) {
            case 'id':
                return $attachment_id;
                break;
            case 'title':
                // return return ("[wpv-post-title item='$attachment_id']"); 
                return wpv_do_shortcode("[wpv-post-title item='$attachment_id']");
                break;
            case 'alt':
                // return ("[wpv-post-field name='_wp_attachment_image_alt' item='$attachment_id']"); 
                return do_shortcode("[wpv-post-field name='_wp_attachment_image_alt' item='$attachment_id']");
                break;
            case 'caption':
                // return ("[wpv-post-excerpt output='raw' item='$attachment_id']"); 
                return do_shortcode("[wpv-post-excerpt output='raw' item='$attachment_id']");
                break;
            case 'description':
                // return ("[wpv-post-body view_template='None' item='$attachment_id']"); 
                return do_shortcode("[wpv-post-body view_template='None' item='$attachment_id' output='raw']");
                break;
        }
    }
}
add_shortcode( 'media-info', 'media_info_func' );

And this is used in a Content Template:

[types field='wpcf-media-credit' item='[media-info field="wpcf-media-credit" info="id"]'][/types]

I even added a case for image-credit but that didn't do anything. It seems like the id is not available to be passed into the shortcode.

On the website, the best example post is:
hidden link
(here you see that for featured it works, but not for the rest)

The content template:
hidden link

The image:
hidden link
Here you see the Custom Fields for Media, Credit field.
Field slug is media-credit (wpcf-media-credit)

#2051267

Thanks for the details, I can login into your website.

Please edit the problem post you mentioned above:
hidden link
There isn't any custom field "media-credit", this custom field is setup in another post type "Media":
hidden link

Is there any relationship between the post and field "media-credit"?
If there isn't, you can setup a one-to-many or many-to-many relationship between "Post" and "Media":
https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/

Then in single post, you can query the related "Media" posts, and display "Media" post's information:
https://toolset.com/course-lesson/displaying-related-posts/#displaying-many-related-items

#2056251

Hi Luo,

I worked with Jamal before, and he had a solution for this. Have you clicked the links I shared that link back to your forum? Here it is explained.

Correct, hidden link does NOT have a media-credit field. This is on the Media Post Type, the standard library from WordPress to house all images.

It seems kinda counter productive to setup a relationship between Post and Media to accomplish this. As mentioned before, Jamal has a solution for this, I just had some issues with it, hence me opening this post again.
https://toolset.com/forums/topic/alt-text-output-for-images-from-custom-field/#post-1857657

To summarize, on Media, there is a credit custom field. Media is the standard Post Type from WordPress. For the featured image I can show it inside a Post (again Standard WordPress), but for any images that have been inserted, I'm having some struggles. The post as shared by Jamal seems to have the solution for this, I just had some issues.

#2057181

Yes, I have already checked the thread you mentioned above, the custom shortcode [media-info] provided by Jamal works only for single instance field, and it output only one image information, it won't output multiple images information.

You need to setup relationship between "Post" and "Media":
https://toolset.com/forums/topic/display-custom-fields-i-added-to-the-standard-media-post-type-in-a-single-post/#post-2051267

#2058795

It would be a single instance field, and not repeating. And I was trying to get this done with one image inside the post, but that didn't even work.

Shame that apparently this is possible with the featured image, but not with images inside the post. Setting up a relationship between Post and Media is going to be a lot of extra work for something that I would expect could just work more or less out of the box just like with Featured Image.

#2059171

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out:
- the problem page URL
- Where I can edit your PHP codes,
I need a live website to test and debug

#2066805

Wait, so it is possible? Kinda confused now.

#2067043

Yes, it is possible with custom codes, but it is not a built-in feature, it won't be easy to manage them,