Skip Navigation

[Fermé] establish relationship between 2 custom post types

This support ticket is created Il y a 7 années et 2 mois. 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 – 10:00 - - - - -
- - - - - - -

Supporter timezone: Africa/Cairo (GMT+02:00)

Marqué : 

This topic contains 9 réponses, has 2 voix.

Last updated by Mohammed Il y a 7 années et 2 mois.

Assigned support staff: Mohammed.

Auteur
Publications
#333831

I have 2 CPT's: Organization and Location. Organization is a parent for Location. I created the posttype using WP Types. I use the WP All Import plugin to populate the posttypes seperately.

I would like to establish the relationship between the 2 posttypes after I imported them because it is not possible to do this while importing them.

Both posttypes have a field orgid what can be used to establish the relationship. So my question is how to establish this connection after the import. Can I do this with views or through php code and if so how?

#333845
Selection_009.png

Hello Luit,

Thank you for contacting Toolset support,

Do establish a relationship between Custom post types ,
- Go to Types > Post Types
- Edit the custom post type
- Scroll down to the bottom of the page to the "Post relationship"
- You will find Parent and children labels
- Click edit in front of parent to select the CPT parent or Children to select CPT children . See image (Selection_009.png)

Thanks.

#333849

I know how it works manually, but with 600 Organizations and 850 Locations it's not something you would do by hand. This had to be done programmatically,

In plain english something like:

for each location where location.orgid = organization.orgid:
location._wpcf_belongs_organization_id' = organization.postid
end.

I hope you understand what I need.

#333850

Hi Luit,

Yes, this big number of posts will need some code So, I will need a database dump from your website to test the solution.

I set the next reply to be private, Which means that only me and you can access it so that you can share a link to the database dump .
You can upload it to Google Drive or Dropbox or anything else.

Thanks.

#333854

The CPT's are Organisatie and Locatie.

#333938

Hi Luit,

I have developed custom code for your issue but after I test it I found that some locations are children of multiple organizations which leads to a many-to-many relationship.

A many-to-many relationship requires intermediate objects to connect locations with organizations.

Do you confirm that ? If so, I can develop another solution to fit your requirements.

Solution details:
- Create a new custom post type (org-loc)
I will create a new post of type(org-loc) that connects organization(s) and location(s) every time I don't find a post of this types links a location with an organization.

Please let me know if this solution fits your needs.

Thanks.

#333939

I checked and I think you are right. Some locations can be used by multiple organiations. I would also be interested in the code you have written so far. Can you share this with me for learnng purposes?

Thanks

#333944

Hi Luit,

This is the code , to be honest we don't offer custom code to our clients but I tried to help you .
If you want to test this code, Please test it in a testing website because you will find a lot of organizations with no locations.

If you want to test this code, put it in the functions.php file of your currently active theme and go to the organizations page hidden link
Then remove it once the page fully loaded.

You will see something like a log so that you can know which location is connected to which organization.

if($_GET['post_type']=='organisatie'){
 $loop = new WP_Query( array( 'post_type' => 'organisatie','posts_per_page'=>-1) ); 
    
while ( $loop->have_posts() ){ 
	$loop->the_post();
    //echo the_title();
    //echo '<br>';
    $organizationsLoop= clone $wp_query;
    $locations=explode('|',get_post_meta($post->ID,'wpcf-org_locids',true));
   // var_dump( $locations).'<br>';
    $organizationId=get_the_ID();
    $organizationTitle=get_the_title();
     echo '<br>==Getting Organization '.$organizationTitle.' locations===<br>';
    foreach($locations as $location){
		$location=trim($location);
		if($location!=''){
	//	echo $location.'-';
		$args = array(
		  'post_title' => $location,
		  'post_type' => 'locatie',
		  'posts_per_page' => 1
		);
		$my_posts = new WP_Query($args);
		
		global $wpdb;
		$res =  $wpdb->get_results("SELECT * FROM wp_posts where post_type='locatie' AND post_title='".$location."' limit 1");
		//echo $organizationId;
		$res=$res[0];
		update_post_meta($res->ID,'_wpcf_belongs_organisatie_id',$organizationId);
		echo 'Location '.$res->post_title.' is now child of '.$organizationTitle.' organization'.'<br>=========================<br>';
		}
		
		}
    
    $wp_query=clone $organizationsLoop;
}
exit;
}

I hope that helps.

Thanks.

#334041

Works like a charm thanks! One more question, I would like to get the code under a button, how can I do that?

#334043

Hi Luit,

Glad to hear that your issue is now resolved, Could you please elaborate more with this "I would like to get the code under a button" . Do you want to execute this code when you click a specific button?

If so,I'm sorry to say that this is not supported by Toolset right now.

But I think this discussion may help : http://stackoverflow.com/questions/13813805/how-to-add-a-button-to-a-custom-post-type-in-wordpress

If you can't do that , I suggest contacting our Certified Partners (https://toolset.com/consultant/) for a custom solution. They might help building such a solution for you.

Thanks.

Le sujet ‘[Fermé] establish relationship between 2 custom post types’ est fermé à de nouvelles réponses.