Skip Navigation

[Resolved] Link between the ID of a page and the ID of its repeatable field

This support ticket is created 2 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by quentinN 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2412153
Capture d’écran 2022-07-06 à 10.10.10.png

Hello,

I'm stuck between the IDs of my posts.

I would like to make a breadcrumb trail.
I have Pages and Articles that list via Customs Posts the IDs of "children".
In the construction of my breadcrumb trail, I would like to: query the database (sql) from the child page/articles to know if its ID is listed in another page (which would be its parent).

Example: I have a page ID = 970 which lists in a custom post the child page ID = 977.
The problem I encounter is the following: when I search the database (SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` LIKE '977';), I get ID = 972.
It turns out that 972 is none other than the ID of one of the repeatable custom fields in the page ID = 970 : we are almost there... but we still need to make the link between the ID of the page (970) and the membership of the repeatable field (972).

Can you tell me in which table of the WordPress database is it possible to prove the link between the ID of a page and the ID of its repeatable field?

#2412177

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I wonder why you use LIke clause to compare the meta value with your SQL query - is there any specific reason behind it?

What if you use equal to instead of Like:

 (SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` =  '977';)
#2412259

Hello,

Yes you are probably right, it is more optimal as a request.

However, it doesn't solve my initial problem:
How to find the link between the ID of a page and the IDs of its Custom Posts Reapetable?

#2412261

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you created any repeatable field group? If yes, can you please share screenshot of that and on what post type you set that repeatable group to be visible/display?

#2412287
toolset.jpg

Here is a capture

#2412309

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok. Repeating field groups are managed as one-to-many post relationship internally.

To loop through the repeating field group, you will have to use the post relationship API function: toolset_get_related_posts()

For example:

global $post;
 $rfg_ids = toolset_get_related_posts(
                                     $post->ID,  // your parent ID
                                     'repeating-field-group-slug',
                                      'parent',
                                      100,
                                       0,
                                       array(),
                                      'post_id',
                                      'child'
                                      );

Where:
- Replace repeating-field-group-slug with your original RFG slug

After that you will get the IDs or repeating field group items and you can loop through it.

 if ( $rfg_ids ){
    foreach ( $rfg_ids as $rfg_id){
        $destination_id = get_post_meta( $rfg_id, 'wpcf-rfg-field-slug', true);
}

where:
-replace "rfg-field-slug" with your custom field destination ID slug.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#2412581
Capture d’écran 2022-07-06 à 20.17.18.png

Sorry but I have no result.. without putting the code in error, the page still works but the function does not return any result.

Is it possible to find the ID of the page that lists (via the custom ID Destination field) the current one via the database?

#2412591

My SQL query is able to find the ID of a repetable field which mentions the ID of the current page ---> I just have to find the ID of the page which has this same ID of repetable field obtained previously.

EXAMPLE:
SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = 977 ---> Returns '972' which is none other than the ID of the custom field contained in the page '970'
So I would like to refine my query so that it returns '970' directly when I enter the page ID '977' as a parameter, which is none other than its child page

#2412955

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I will require access details and I will require to know how you add parameter from where and then after reviewing your structure I will be able to guide you in the right direcgtion.

Please share details what ID or field you want to use where and what would be the result you want at the end with test case example. Please share what file you are using where you added the code.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2413033

One of the "deepest" URLs is this one: hidden link (ID = 977)
We are supposed to find for the breadcrumb trail : 53 > 201 > 536 > 970 > 977

#2413379

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've changed the code within the Ariane.php file as given under:

$ID_actuelle = $post->ID;
						$Fil_Ariane = array($ID_actuelle);
						function Recherche_Parent ($ID_actuelle,$flag=false) {
							global $wpdb;
							global $Fil_Ariane;
							
							
							if(!$flag){
							
							$query = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = ".$ID_actuelle.";";			
							$results = $wpdb->get_col($query);		//echo $query.'<br />';
							
							$parent = toolset_get_related_post($results[0],'un-enfant-listing');
							
							
							
							
							array_push($Fil_Ariane, $parent);
							Recherche_Parent($parent,true);
							
							}else{
								
								if(!$ID_actuelle){
								
									return;
									
								}
								$query = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = ".$ID_actuelle.";";			
								$results = $wpdb->get_col($query);		//echo $query.'<br />';
								
								
								if(count($results) > 1){
									
									foreach($results as $k=>$v):
										$parent = toolset_get_related_post($v,'un-enfant-listing');
										if(!$parent){
											continue;
										}else{
											array_push($Fil_Ariane, $parent);
											Recherche_Parent($parent,true);
											
										}
										
										
									endforeach;
									
									
									
								}else if(count($results) == 1){
									$parent = toolset_get_related_post($results[0],'un-enfant-listing');
									array_push($Fil_Ariane, $parent);
									Recherche_Parent($parent,true);
								}
								
								
								
							}
							
						
						}
						// INIT - On execute la function
						 Recherche_Parent($ID_actuelle);
						$Fil_Ariane = array_reverse(array_diff($Fil_Ariane,[0])); 
						print_r($Fil_Ariane);

As this is a custom code this is the max help we can offer, you can see the result with your page:
- hidden link

I hope the solution I shared will help you to resolve your issue or you can adjust the above code as required or call our certified contractors for any further help related to such custom edits:
- https://toolset.com/contractors/

#2413649

Whoa! Wonderful! It is perfect.

Thank you so much for your help and the clarity of the code!

Quentin