Skip Navigation

[Resolved] Hide in future value of connected relationship field on register form

This support ticket is created 3 years, 3 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
- 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 6 replies, has 2 voices.

Last updated by darioM-3 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2360147
Screenshot 2022-05-10 at 22.28.00.png

Tell us what you are trying to do?

I make relationships one to one (StickerID post type to Woo Product). After that, I make a form and put relationship field "Stickers ID" to connect Sticker ID to Product. Everything working fine. I pick up the normal StickerID value (teststicker) and connect it to the Product. But later again when I want to connect the new StickerID to another product it shows also already exists (teststicker) connected in the relationship field. I can pick it up again teststicker. Is it possible that somehow when I connect StickerID (teststicker) later does not display anymore in the relationship StickerID field? Maybe use some conditional or?

Thank you.

Is there any documentation that you are following?
Not found

Is there a similar example that we can see?
Not found

What is the link to your site?
it is under construction.

#2360405

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

There is no native filter available to filter the post relationship box results but there could be workaround but I will need to check if that will work for one-to-one post relationship.

Can you please share problem URL as well as admin access details and also share for what post you want to remove the (teststicker) from post relationship dropdown.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2360791

Minesh
Supporter

Languages: English (English )

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

I've added the following filter code to "Custom Code" section offered by Toolset:
=> hidden link

function filter_related_posts_exclude_parents( $query ) {

  if(defined('DOING_AJAX') && DOING_AJAX 
     && $_REQUEST['action']=='select2_potential_relationship_parents'
     && $_REQUEST['slug']=='sticker-id-product-cykler'
     && $_REQUEST['form_type']=='edit'){ 
   
   	 	$edit_post = get_post($_REQUEST['cred_post_id']);
   
   		$exclude_parents = array();
        global $wpdb;
     	
    	if($edit_post->post_type=='product') {
         
          $child_post_slug = 'product';
          $relationship_slug = 'sticker-id-product-cykler';
          
          $childs = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} pm
           										WHERE pm.post_type='".$child_post_slug."' and 														pm.post_status IN ('publish', 'draft')"));
          
         foreach($childs as $k=>$v):
              $has_relation = toolset_get_related_post($v,$relationship_slug,'parent');
              if($has_relation !== 0){
                $exclude_parents[] = $has_relation;
              }
          endforeach;
          
          	if(!empty($exclude_parents)){
    	 		$query->set("post__not_in",$exclude_parents); 
        	}
 	 }
   }
}
add_action( 'pre_get_posts', 'filter_related_posts_exclude_parents', 101, 1 );

Could you please check now if it works as expected:
- hidden link

Another thing is it possible that the relationship field in this case Stickers ID be the input field (not search dropdown like it is now)?
==>
No, the relationship field will be automatically displayed as dropdown.

#2360837

Hello support,

Working perfectly on the edit form, but I have a register bike form or create which also has a custom field sticker-id-product-cykler:

hidden link

Is this possible that there also the same?

Many thanks.

#2360865

Minesh
Supporter

Languages: English (English )

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

Could you please check now: hidden link

I've adjusted the code as given under to "Custom Code" section:

function filter_related_posts_exclude_parents( $query ) {

if(defined('DOING_AJAX') && DOING_AJAX 
     && $_REQUEST['action']=='select2_potential_relationship_parents'
     && $_REQUEST['slug']=='sticker-id-product-cykler'
     && isset($_REQUEST['form_type']) && in_array($_REQUEST['form_type'],array('new','edit')) ){ 
        
   	 	$edit_post = get_post($_REQUEST['cred_post_id']);
   
   		$exclude_parents = array();
        global $wpdb;
     	
    	if($edit_post->post_type=='product') {
         
          $child_post_slug = 'product';
          $relationship_slug = 'sticker-id-product-cykler';
          
          $childs = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} pm
           										WHERE pm.post_type='".$child_post_slug."' and 														pm.post_status IN ('publish', 'draft')"));
          
         foreach($childs as $k=>$v):
              $has_relation = toolset_get_related_post($v,$relationship_slug,'parent');
              if($has_relation !== 0){
                $exclude_parents[] = $has_relation;
              }
          endforeach;
          
          	if(!empty($exclude_parents)){
    	 		$query->set("post__not_in",$exclude_parents); 
        	}
 	 }
   }
}
add_action( 'pre_get_posts', 'filter_related_posts_exclude_parents', 101, 1 );

Where:
I changed the if condition from:

 if(defined('DOING_AJAX') && DOING_AJAX 
     && $_REQUEST['action']=='select2_potential_relationship_parents'
     && $_REQUEST['slug']=='sticker-id-product-cykler'
     && $_REQUEST['form_type']=='edit'){ 
    

To:

if(defined('DOING_AJAX') && DOING_AJAX 
     && $_REQUEST['action']=='select2_potential_relationship_parents'
     && $_REQUEST['slug']=='sticker-id-product-cykler'
     && isset($_REQUEST['form_type']) && in_array($_REQUEST['form_type'],array('new','edit')) ){ 

I can see its working as expected. Could you please confirm it works at your end as well.

#2360875

My issue is resolved now. Thank you!