Skip Navigation

[Resolved] Looking to use gallery slider with custom post content view

This support ticket is created 4 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: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Robert 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1622061
image.png

Tell us what you are trying to do?
Currently, I have 10 custom image fields on a post type. Each is a non-repeatable field. I originally created 10 because I wanted to control the "featured" image of the post versus the other 9 images. I also wanted to be able to limit the number of images a user could upload.

Is there any documentation that you are following?
Just the standard toolset documentation.

I currently allow users to post content via a toolset post form that contains these 10 fields. My problem is that the users can remove image links via a" clear value" or "replace image" button on the post edit form but cannot remove the images from the post. For example: Lets say a user adds 6 images, one in each image field. The user then replaces each of those 6 images with new images. The post edit form will only show those newer 6 images but the post itself with the [gallery] shortcode will display 6 old images plus the 6 new images. It never really clears the images.

I don't know how to correct this except by creating a new repeatable image field going forward. But repeatable fields don't work with the [gallery] shortcode so I will have to create a view in order to display/format my custom gallery based on the repeatable image field. But then I need to figure out how to move the old images of each post into the new into the new repeatable image field.

I am quite experienced in SQL so I may be able to do some table updates if needed. Is it as simple as changing meta keys for the old image fields to the repeatable image field?

#1622213

Follow up:
So here is what I figured out so far:

1. If I delete images from a repeatable custom image field or a single custom image field, those images won't appear on the edit post type form as they have been removed from the custom field in the meta data BUT the attachment is still maintained as a child to the post. So using the [gallery] shortcode will still display those images. This means that I will have to use a View to display only the images in those fields instead of [gallery]. Any reason why removing the images from a custom field doesn't just delete them altogether?

2. I can convert existing custom image fields to a repeatable custom image field simply by performing an update to the meta_key. For example SET meta_key = 'wpcf-repeat-image' WHERE meta_key = 'wpcf-single-image-1'.

3. What would solve my problem is to limit the number of times a field can be repeatable. I don't want users given the ability to infinitely upload images. Is it possible to place a condition on the repeatable field to limit the number of repeatable images to 1, 3, and 10? Currently I use the GROUPS plugin so adding shortcode to determine which group a user belongs to will correspond to whether they should upload a max of 1, 3 or 10 images but I don't know how to limit the repeatable field.

#1622999

Hello and thank you for contacting the Toolset support.

From what I understand so far, there are two problems involved, please correct me if I am wrong:
- Deleted images from Toolset fields still appear in the result of the [gallery] shortcode.
- Limiting the instance of a repeatable image field.

I'll try to discuss both cases below and suggest solutions for each. If I missed something, let me know about it or create a new ticket for it.

Deleted images from Toolset fields still appear in the result of the [gallery] shortcode.
I believe it is a WordPress behavior rather than a Toolset one. When you attach an image to a post(inside the editor, or as featured image, or using Toolset), the image is saved in the database as an attachment and the parent_post is the post where it is added. But when the image is not used anymore, the parent_post is not removed, that's why it still appears in the [gallery] shortcode results. You can remove these unattached images manually or using a plugin:
- hidden link
- https://wordpress.org/plugins/media-cleaner/

As a solution, I would suggest using the "include" attribute of the [gallery] shortcode.

[gallery ids="729,732,731,720"]

I'll explain later how to get the ids from a repeating field and you can adapt it to use it for a regular image field.

Limiting the instance of a repeatable image field.
There is no built-in solution for this. But you can limit the form to a min-max number of instances with custom Javascript. Check these forum threads, you may adopt/adapt their solutions to your case:
- https://toolset.com/forums/topic/limit-number-of-instances-for-two-different-custom-fields/
- https://toolset.com/forums/topic/repeaters/

If you use a repeating field and you would like to get the ids of the images for the [gallery] shortcode, you will need to create a custom shortcode that will calculate it. Check this forum ticket https://toolset.com/forums/topic/get-id-of-repeater-image-field-insted-of-url/

I hope this helps. Let me know if you have any questions.

#1623175

Thank you! I will look to set both of those up and get back to you.

I have another question related to this around repeatable custom field groups. I assume that the custom field groups is really just a related post type as a child to the parent post type, correct? If so, do you have a recommendation to convert existing images from the custom fields into the repeatable custom field groups? I am considering the typical repeatable custom field group of: Image, Caption, Description.

#1623197

Custom field groups is a structure of custom fields saved and used by Toolset. Custom fields on the other hands are saved as meta for a post(wp_postmeta table) instead of being saved as a post(wp_posts table). Repeatable custom fields are saved multiple times in the database for the same custom meta key. Check this screenshot for my repeatable custom field 'book-images'. Notice that it is prefixed by Toolset with "wpcf-" hidden link

I would suggest creating a new custom field for images, the field needs to be repeatable. And then change in the database the meta key for all your images fields. A query similar to this for each of your images fields

UPDATE `wp_postmeta` SET `meta_key` = 'new-images-field-slug' WHERE 
post_id in (SELECT id FROM `wp_posts` where post_type='your-custom-post-type-slug')
and `wp_postmeta`.`meta_key` = 'old-image-field-1-slug';

You should adapt this code depending on your database prefix (wp_), custom post type(your-custom-post-type-slug), new repeatable custom field(new-images-field-slug) and each of your old image fields(old-image-field-1, old-image-field-2, etc.)

#1623251

And if I decide to use the custom field repeatable group on my custom post type instead of just repeatable image field, any suggestions for converting the existing images to the group? I am assuming this that the image within the repeatable group is a child of the group and then the group is a child of the post.

#1623255

Repeatable groups are different. They are not stored as custom meta, but as posts(wp_posts).
A query to convert these fields will be very complex.

Is that the case for you? Were you using Repeatable Groups or Repeatable Fields ?

#1623267

Thanks. I definitely want to move to a repeatable image field but have been debating moving to the grouped repeatable field so I can give users image captions and descriptions in addition to image BUT....that does seem more complex on conversion so I think I will stick with the repeatable field only. Thanks so much for your help! Both the conversion of existing images update and Js for limiting repeats are perfect. 🙂

#1623269

My issue is resolved now. Thank you!