Skip Navigation

[Resolved] get image ids of repeatable fields via PHP in Oxygen Builder

This support ticket is created 3 years, 10 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 15 replies, has 2 voices.

Last updated by Shane 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#2097107

Tell us what you are trying to do?
Getting image ids of repeatable fields via PHP in Oxygen Builder via the code proposed
but it doesn't produce an output via do_shortcode. I created the shortcode with a custom plugin.
here is my code trying to get the output:
$para = do_shortcode("[get_image_id id='" . get_the_ID() . "' field='wpcf-bilder']");

Is there any documentation that you are following?
https://toolset.com/forums/topic/get-the-image-id-of-the-repeatable-image-fields/
Is there a similar example that we can see?
unfortunately no
What is the link to your site?

#2097135

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Given that you are doing this in purely PHP then you have no need for the user of the [get_image_id] shortcode.

You can implement the code directly in your template.

$post_id = get_the_ID();
$image_urls = get_post_meta($post_id,'wpcf-bilder');
$id_list = array();
    foreach ($image_urls as $image_url) {
        if(!empty($image_url)){
                 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
                 array_push($id_list, $attachment[0]);
             }
        }
          
 $para = implode(",", $id_list); 

This should be able to help you, $para should have the list of ID's

Thanks,
Shane

#2097181

Hi Shane,

thank you very much for your help - unfortunately this code doesn't work either.
Don't know why.

I am trying to build it into a template that is built with Oxygen Builder.

It looks like it doesn't find the repeatable group as the array seems to be empty when I echo it.
The slug in Toolset is "bilder" but it doesn't work wether I use "wpcf-bilder" nor just "bilder".

#2097223

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

In a case like this we need to know if the correct data is being passed along.

Can you do a var_dump on the $post_id variable as well as the $image_urls variable.

This is to check to see if the data that we are expecting is actually being passed. For the $post_id ID variable can you confirm that the ID that is dumped out on the page is the correct ID of the current post being viewed.

Then we can take it from there.

Thanks,
Shane

#2097267

Hi Shane,

a var_dump shows me that it gets the ID properly, but the array your code should propduce is empty.

Thanks!

#2097743
#2097829

This is the new code with the new approach:

$post = get_post( get_the_ID() ); // the parent post that holds the RFG
$rfgs = toolset_get_related_posts(
$post, // the parent post
'bilder', // the RFG slug
'parent', // the post role in this relationship is 'parent'
10000, // the maximum number of results
0, // the offset
array(), // additional query arguments
'post_id', // return format
'child', // role to return
);

echo 'rfgs: ' . var_dump($rfgs);
$id_list = array();
foreach($rfgs as $cpt){
echo 'cpt: ' . var_dump($cpt);
$cptpost = get_post($cpt);
echo 'cptpost: ' . var_dump($cptpost);
$image = get_post_meta($cptpost,'bild');
echo 'image: ' . var_dump($image;
array_push($id_list, $image);

}

$para = implode(",", $id_list);
var_dump($id_list);

Still not working as it doesn't grad the image id from the Repeatable Field Group.

#2098049

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

The code above is for Post Relationships. This is meant to be used when there is a relationship created between two post types.

What I need clarity on here is if this is a repeatable field or is it a repeatable field group because the code will be changed based on that assumption.

If it is a repeatable field group then this will complicate things because we will first need to loop through each of the fields in the repeatable field group and then collect the IDs of the images in each of them and store it in an array. So before we go any further I will need to know if it a Repeatable field Group or Repeatable Fields.

Perhaps you can send me a screenshot of the fields on the backend as well.

Thanks,
Shane

#2098131
Bildschirmfoto 2021-06-24 um 17.19.54.png

Hi Shane,

it is a Repeatable field group.
Is it possible to convert that into repeatable fields somehow?
If not, then yes, the code is more complex.

Here is login data:
andreas.convernatics.com/wp-admin
user: toolset_shane
password: sgvt&ZW1Ejns4GsEDyxDq2cy
Adress of the oxygen template: hidden link

the code is in the code block in the middle section if you want to try something, feel free, it is a development site.

Thanks!

#2098291

Hi,
I think I already answered but somehow your ticket-system doesn't show that, so to be sure, I'll write a second time:

it is a picture in an repeatable field group.

Login data can be provided if needed (did that in the answer before this one, but not sure if it was registered by your ticketing system)

Thanks!
Peter

#2098349

I got this now, after quite some work:

$id_list = array();
$child_posts = toolset_get_related_posts( get_the_ID(), 'bilder', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );

foreach ($child_posts as $child_post) 
{ 
	$image = types_render_field( "bild", array( "id"=> "$child_post->ID", "size" => "full", 'url'=>'true' ));
	$attachment_id = attachment_url_to_postid($image);
	
	array_push($id_list, $attachment_id);
}
	$para = implode(",", $id_list);

this way I get the list of image ids that I needed.
The shortcode doesn't work though, but I don't think that that would be a Toolset problem.

#2098361

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Sorry I couldn't get on this one faster as the forums are quite busy at the moment.

Given that you've gotten it to work, shouldn't you now have a list of the id's in the $para variable?

Are you passing the list of id's into another function ? Or a custom code ?

Please let me know so that I can perhaps make a recommendation.

Thanks,
Shane

#2098365

Hi Shane,

I am passing the variable to a do_shortcode of a gallery shortcode.

Whole code looking like this then:

$id_list = array();
$child_posts = toolset_get_related_posts( get_the_ID(), 'bilder', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );

foreach ($child_posts as $child_post) 
{ 
	$image = types_render_field( "bild", array( "id"=> "$child_post->ID", "size" => "full", 'url'=>'true' ));
	$attachment_id = attachment_url_to_postid($image);
	array_push($id_list, $attachment_id);
}
	$para = implode(",", $id_list);
	var_dump($para);
echo do_shortcode('[oxy-ou_acf_gallery ct_sign_sha256=\'94482d78e1e9cdcf2a2374c06b050c02f60a86c1caeeaadd48f1a48e6337194f\' ct_options=\'{"ct_id":99,"ct_parent":0,"selector":"-ou_acf_gallery-99-816","original":{"oxy-ou_acf_gallery_ouacfg_source":"media","ouacfg_images":"'. $para .'","oxy-ou_acf_gallery_acfg_col":"25"},"activeselector":false}\'][/oxy-ou_acf_gallery]');

Unfortunately this doesn't work and I think it is because of all the ' and " in there. This turns out to be a very tricky thing to do, while I thought it would be fairly easy.

#2098399

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

I may have spotted the issue, you perhaps don't need the single inverted commas to concatenate the variable with the string. Given that the double quotes would already allow it to convert to string values.

Try it like this.


echo do_shortcode('[oxy-ou_acf_gallery ct_sign_sha256=\'94482d78e1e9cdcf2a2374c06b050c02f60a86c1caeeaadd48f1a48e6337194f\' ct_options=\'{"ct_id":99,"ct_parent":0,"selector":"-ou_acf_gallery-99-816","original":{"oxy-ou_acf_gallery_ouacfg_source":"media","ouacfg_images":". $para .","oxy-ou_acf_gallery_acfg_col":"25"},"activeselector":false}\'][/oxy-ou_acf_gallery]');

Perhaps this should resolve your issue now.

Thanks,
shane

#2098497

Hi there,
unfortunately the : in the shortcode cause issues which is why the ' are needed.
This one is really hard.