0
votes

I'm currently developing a plugin for internal use at my company. What I'm trying to do is create an image gallery that gets it's content from our digital asset management company. I can access the collections from our DAM no problem and return an array with all the information I need (i.e. image title and image URL) and create a custom post type with a click of a button in the plugin admin page. The part I'm getting stuck on is 2 fold.

  1. On post creation (or update)create a custom meta box for each item in the array and...
  2. loop through the array and add the array value's to the meta input fields.

Essentially if there's 5 images in a returned array, the custom post type should have 5 meta boxes(or 1 box with 5 sets of fields), each containing the single image info (title/URL).

I'm sure jQuery needs to be leveraged within my add_meta_box callback function, I'm not sure the appropriate steps. What I'm looking for here is advice or a point in the right direction. I do have code but I don't believe it will be helpful at this point. I can post if needed. Thank you in advance everyone who takes the time to help.

1
Will there always be atleast one image for each created post?Faham Shaikh
Yes at the very least 1 image will be in every custom post.user1568811
You don't need js in that case. I will add my answer in a minute.Faham Shaikh

1 Answers

0
votes

Assuming that all of the images are in single meta key, the approach would be to leverage global post variable available with us.

Simply wrap your meta box in if condition like:

global $post;
if (get_post_meta($post->ID, 'myMetaKey', true)) {
    add_meta_box('my_metabox_id', __('My Meta Box', 'my-text-domain'), array($this, 'my_metabox_loader_callback_function'), 'post', 'normal', 'low', null);
}

Even if you have all values in separate meta keys only your if condition will change to check if atleast one value is available. like this:

global $post;
if (get_post_meta($post->ID, 'myMetaKey', true) || get_post_meta($post->ID, 'myMetaKey1', true) || get_post_meta($post->ID, 'myMetaKey2', true) || get_post_meta($post->ID, 'myMetaKey3', true) || get_post_meta($post->ID, 'myMetaKey4', true)) {
    add_meta_box('my_metabox_id', __('My Meta Box', 'my-text-domain'), array($this, 'my_metabox_loader_callback_function'), 'post', 'normal', 'low', null);
}

In the callback function you can display the title and image based on your meta key and I hope you will now be successful in getting this to work.