Skip Navigation

[Resolved] relate post types by common field?

This support ticket is created 6 years, 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#590176

Tell us what you are trying to do?
Can I relate posts similarly to child/parent relationships but by using a common field name and type? eg. Vet Practice Name, which is common to Vet Posts and Practice Posts.

Is there any documentation that you are following?
Nope

Is there a similar example that we can see?
Not that I've seen

What is the link to your site?
hidden link

#590207

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Cam

If you have a Vet Practice post type, yes, you could have a custom field on your Vet Posts and your Practice Posts where you add the Vet Practice they belong to, either storing the practice name (or slug, perhaps) or the post ID of the Vet Practice.

But you are effectively managing the connections yourself and some of the UI created to support post relationships won't be available to you.

It is still possible to, for example, create a View which shows Vet Posts for this Vet Practice post, but instead of adding post relationship filters you would add a filter for the Vet Practice field. To dynamically set the Vet Practice it belongs to you can use a shortcode attribute that you add to shortcode you would use to insert the View.

So you might insert the View to list Vet Posts with a "connect" attribute like so:

[wpv-view name="vet-posts-belonging-to-a-vet-practice" connect="[wpv-post-id]"]

Your View then includes a filter that uses that shortcode attribute.

So, in sum, yes, you can, but you have more work to do to make use of the post relationships.

#590376

I'm happy to do it as you suggest as I'm trying to automate my post imports and preserve some kind of relationship without having to use the GUI.
I'm importing thousands of posts daily so having some means to create a related post would be awesome.
I'm rushing right now so will re-read your suggestion and give it a go when I'm back from my meeting.

#590469

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Cam

If the reason for this is to add post relationships when importing data, depending on your source data, and whether the parent posts already exist in the database or can at least be imported first, you might still be able to use Types post relationships.

See this thread for a description of using WP All Import Pro to import posts and automatically assign post relationships... https://toolset.com/forums/topic/automatically-linking-child-posts-during-wp-allimport-pro/

#590499

Yep, you've got it. That's exactly what I'm trying to do and I'm already using All Import Pro, but couldn't figure out how to make link on import. I can set up my imports using a Cron Job and just schedule the parent posts first.
I'll have a go at following their instructions and see if I can manage it. Woo! Keep you posted.

#590511

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, do let me know how you get on and feel free to share anything in particular you do to get it working for the benefit of other users.

#590519

I kinda get what they're doing, but I'm not sure how to implement it as the detail is a bit sketchy for me.
I've left this question on their post, but goodness knows if they'll reply. Can you figure out how to modify it so it works in my case? To technical for my limited knowledge.

"I'm importing two different record sets each day to two different post types.
One parent post (Vet Practices) the other a child post (vets).
The only common field between the two imports is 'Practice Name' and that's the key to link the right vets to the right practice.
How would I change your script to make it work for my situation?..."

#591110

Any more thoughts Nigel? I'm stuck without help at this point but I feel that solution on that other post is nearly what I need. If some one could just tell me/us how to modify that script!!!!

#591142
Screen Shot 2017-11-20 at 10.48.39 PM.png

See attached.
I got this far, but it doesn't seem to work...

#591214

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Cam

Sorry, our support policy rules out providing custom code, but let me give you some pointers.

If you are trying to import the posts such that Vet Practice posts are linked in a parent-child relationship with Vets, then you should know that that relationship is recorded like this:

The child Vet posts will have a hidden custom field of _wpcf_belongs_vet-practice_id that records the ID of the parent Vet Practice post. (I'm assuming here the slug of the Vet Practice post type is vet-practice.)

So you would first import the Vet Practice posts so that they are created in the DB with a post id.

Then you add the custom function to use when importing the child Vet posts which runs a query to find the existing Vet Practice post which has a custom field Practice Name (if this is a Types custom field it will have a key of 'wpcf-practice-name' or similar) the same as the post being imported, the ID of which is what is stored for _wpcf_belongs_vet-practice_id.

So the new WP_Query used in your function needs to get posts of the type Vet Practice with a meta query to match the practice name with the value coming from the post being imported.

There are lots of tutorials and examples for performing WordPress meta queries if you are unsure how, and the source documentation is here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

If you update your code and are still stuck, let me know.

#591507

Thanks Nigel, but I've had another go, without any luck.
Honestly I've given it a good go, but I'm no coder.
Any other pointers greatly appreciated. Honestly, it's just on test server.
So there's little risk to break anything.
Many thanks
Cam

#591578

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Cam

What specifically are the slugs of the custom post types involved?

#591865
Screen Shot 2017-11-22 at 2.47.37 PM.jpg
Screen Shot 2017-11-22 at 2.47.07 PM.jpg
Screen Shot 2017-11-22 at 2.46.18 PM.jpg

Hi Nigel,
Slug for practice names in Vet Practices and Vets is practice-name.
I've attached screen shots of the Toolset custom field names.

I set it up the script like this below but it doesn't work;

<?php
function get_parent_id( $sample_id ) {
$slug = strtolower( $sample_id );
$query = new WP_Query(
array(
'practice-name' => $slug,
'post_type' => 'Vet Practices'
)
);
$query->the_post();
return get_the_ID();
}
?>

#592049

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Cam

I started to think of a solution, but I don't know what data is being made available to the custom function from WP All Import without having access to the source data files and setting this up for myself with a copy of WP All Import Pro, and doing that to provide a custom code solution is outside our support policy I'm afraid.

If you are unable to code it yourself you should contact a developer who is familiar with WP All Import to do it for you. You can contact the list of Toolset Contractors here: https://toolset.com/contractors/

I'm sorry I can't do it for you.

#592389

I can give you access to the site if you want?
One of the previous moderators has done that sort of thing for me before... just so they can get a look at how the site is set up. Made life a lot easier.
Otherwise, yea... I'm bit buggered really. 🙁

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.