Skip Navigation

[Resolved] Relation between 2 custom fields (hierarchical)

This support ticket is created 7 years, 7 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
- 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)

Author
Posts
#437400

Hi,

I don't undersand well the mechanism of Toolset, taxonomies and relations between CPT.
I must build a new website in WordPress for a customer. The current one is based on a CMS build on my own from scratch in ASP.net a few years ago.
Now, WordPress can do a really more beautiful website with a lot of more capabilities.

My problem : the website allow the visitors to make a search on summer activities.
They can filter on some specific categories of activities, in a specific place, between two prices, ...
All the activities belong to a specific company.
In my SQL Server database, there is a link between an organism and the activities. So visitors could also see all the activities proposed by a specific organism.

I'm trying to make the same with toolset, but I don't really understand.
I created 2 CPT : "Activities" and "Organism".
I created then many taxonomies :
Taxonomies for "Activities" :
- date
- price
- place
- age minimum
- age maximum
....

Taxonomies for "Organism" :
- Name
- phone number
- website
- address
...

How can I do to show easily at the front-end side of the page a dropdownlist that show me all the activities proposed by the organism the visitor chose in this dropwonlist?

By the way, after finding the solution for this.
Is there any way to import my SQL database easily in the wordpress database (because there is a lot of records) to get the real matching between organism and activities?

Regards,

#437641

Nigel
Supporter

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

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

Hi Jeremy

Let me start by listing a few of the initial documentation pages which should help you if you haven't read them already. Note that the last of these relates to importing existing data into WordPress using 3rd party plugins, in answer to your final question.

About custom post types: https://toolset.com/documentation/user-guides/create-a-custom-post-type/

About custom taxonomies: https://toolset.com/documentation/user-guides/create-custom-taxonomies/

About custom fields: https://toolset.com/documentation/user-guides/using-custom-fields/

About importing data: https://toolset.com/documentation/user-guides/how-to-import-content-into-wordpress-using-csv/

Now I'm going to add one last—external—link, and this one is to an article about the relationship between taxonomies and custom fields (or post meta in official WordPress jargon): hidden link

This last link I think gets to the heart of your problem. You are using taxonomies where I think you probably want to be using custom fields.

When you have your custom posts created with appropriate custom fields, you can create Views to display lists of posts, using query filters to narrow down the results based upon the values of your custom fields. (See https://toolset.com/documentation/user-guides/views/)

You can make these work as interactive filters using custom search views (you might see these referred to in other threads as parametric searches which is how we described them before the current release). (See https://toolset.com/documentation/user-guides/front-page-filters/)

If you read through the above you should have a better idea of how to structure your data.

If you have any remaining doubts about that then don't hesitate to ask for some specific guidance.

#439155

Hi Nigel,

thank you for your reply.
I have not read all your link yet, but at this moment, I understood better how I have to do.
I won't come back quickly as I'm busy on other projects, so could this post be kept open during a "long" moment?

Regards

#439266

Nigel
Supporter

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

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

Hi Jeremy

No problem. I'll leave this marked as waiting for further details from you which means it will stay open for up to a month before the forum robot automatically closes it.

#443654

Hi Nigel,

I'm back with another issue, but maybe can't you help me.
Here is my structure for 2 CPT (organism and activity) :
1 organism could propose to customers many activities, but each activities is proposed only by 1 organism.

Organism :
- name (which would be the title of the post)
- address
- city
- ...

Activity :
- IDActivity : in my current database, I got an ID of the organism that propose this activity
- activity_name : which would be the title of the post)
- activity category
- place
- opening hours
- age min
- age max
- handicaped accessible
- ...

I got 2 questions :
- I think that all this fields could be custom fields and no one would be taxonomies. Am I right?
- How to link the activities to the right organism while importing? Should I have to insert in my current database something special?

And I'm facing issue while importing a test "organism" file. The CSV importer tells me that some informations seem to be empty, but they aren't, so I don't understand.
See here : hidden link

Could you help with this?

Regards

#443852

Nigel
Supporter

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

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

Hi Jeremy

Firstly, the file you are importing.

I tried to import the csv file into OSX Numbers and OpenOffice, neither of which correctly parsed the data into columns using the comma as a delimiter, so you may have a formatting issue with that file.

You can certainly import all of those as custom fields, but whether custom fields rather than taxonomies is the best choice partly depends upon what you want to do with the data once you have it imported. I would continue with them as custom fields for now unless you run into a situation where you realise they would have worked better as taxonomies (e.g. activity category?).

You have a problem, which is a common problem, when importing posts that use Types post relationships.

Organism is a custom post type, and when you import organism posts their post_id will be automatically assigned by the database as the posts are added. You don't know what they are before you import them.

The relationship between the parent Organism and child Activity posts is stored as post meta on Activity in a field _wpcf_belongs_organism_id. Even though in your source data you have an 'IDactivity' to store the Organism that is the parent of the Activity, once you import that data that id won't correspond to automatically generated id of your Organism posts.

Unless you are able to write a custom script that can set up the parent assignments after the data is imported, I'm afraid I don't know of any other way than manually connecting the parent and child posts, which could be painful if you have a lot of data.

#444975

Hi Nigel,

once again, thank you for your answer.
I found why it wasn't well parsed (opening with Excel change the data, thank you Microsoft...).

I'll try to import it manually using script as I can have access directly to the database.
In my current database, I have a link (IDOrganism) in the activity table. I'll use it to match, by a way or another, the activities to the organism.
But, I don't know the structure of the tables neither in which table I have to insert data.
You spoke about the '_wpcf_belongs_organism_id'. Is it the only column where I have to insert the data manually. In which table is it?
Is there any other table to fill?

Regards,

#445294

Nigel
Supporter

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

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

Hi Jeremy

You should probably familiarise yourself with the standard WordPress database structure: https://codex.wordpress.org/Database_Description

For posts (whether custom post types or standard WordPress posts, pages, or media uploads), these are stored in the wp_posts table. The post id auto-increments as new posts are added.

Additional post meta, including Types custom fields, are stored in the wp_postmeta table as key : value pairs, with an auto-incrementing meta_id, the post_id which ties the post meta to the post, plus the meta_key and meta_value (a string).

Types custom fields generally are stored with a 'wpcf-' prefix, so if you create a custom field with a slug of 'description' then it would be stored in wp_postmeta with a key of 'wpcf-description'.

Post relationships are currently stored as standard post meta on the child post, with a key of '_wpcf_belongs_parent-slug_id' and a value of the id of the parent post (where 'parent-slug' is the slug of the parent post type).

As I mentioned above, the problem with connecting your parent and child posts when importing is that, if in your source data the child post has an id of the parent post, when the parent post data is imported the auto-assigned post_id's won't match.

When you import your parent posts you could add the existing old id as a redundant custom field, and when you import your child posts store the old parent id in another custom field, and then you could write a script to get the new value of a parent post id for each of the old values and set the _wpcf_belongs_parent-slug_id post meta that way.

#446058

Hi Nigel,

thank you once again.
This help me, and I could import my Organism (I must now do the same for the activities) and I'll script the data to do the relationship.

But, I'm facing another issue now with the templates.
I've imported all my 'activity-categorie' taxonomie in my WordPress database : no problem.
This is a hierachical taxonomy.
I've created a template to view an activity, but when I want to insert the shortcode for my taxonomy (activity-categorie), nothing happens.
I tried with a new taxonomy (created this WP-Type, not imported) and it's the same.
But to insert the shortcode of a custom fields, I have no problem.

Could you help me one more time?

Regards,

#446265

Nigel
Supporter

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

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

content-template.png
custom-taxonomy.png

Hi Jeremy

Here's what is involved making a custom taxonomy and displaying the terms.

I made a custom post type of restaurants and created a custom taxonomy of food-category and assigned it to the restaurant post type.

See the first screenshot.

I then edit the content template for my single restaurant posts and insert the shortcode for the taxonomy term. See the second screenshot.

When I set a food-category on a restaurant and then view that restaurant on the front end I see my assigned taxonomy term.

You should be able to reproduce that yourself when making a custom taxonomy through the Toolset pages in the WordPress admin area.

Importing taxonomies is rather more complicated.

You will need to review that documentation about the database structure again for the taxonomies. Frankly, the WordPress taxonomy database structure is something of a mess, but we are stuck with it.

#446368
8.png
7.png
6.png
5.png
4.png
3.png
2.png
1.png

Nigel,
I don't understant what happens.
Actually, this works only (see screenshot 8) if I insert the shortcode manually (screenshot 7) in my template, without opening the 'fields and taxonomy' selection page. (see the screenshots).
So, it's ok, but I don't understand why I can't see it in the 'taxonomy selection page'.
Did I made something wrong?

#446586

Nigel
Supporter

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

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

Hi Jeremy

I looked through your screenshots and everything appears okay.

You say that you have to enter the taxonomy shortcode manually.

When you use the Fields and Views button (screenshot 5) I can see the button for Catégories d'Activité. What happens when you press it? It doesn't insert the shortcode?

Are there any errors in the browser console when you do that?

#446624

You're right, I see the button for Catégorie d'Activité, but if I click on it, it just close the popup and nothing happens.
Now, I know that it works, but I don't understand why.
I tried with another web navigator but the problem is the same.
So it's linked with the taxonomies.
But if I change the type of this taxonomy from Hierarchical to Flat, the popup propose me the options.

But, I'm now trying to set another custom fields, and I see that there is a "Select" custom fields. So maybe would it be better if I delete all my taxonomies and replace them by a custom field?
What do you think about (sorry for my stupid questions, but I'm clearly new to this).

Regards

#446686

Nigel
Supporter

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

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

Hi Jeremy

It might make sense for you to use custom fields vs taxonomies (you can order query results by custom fields, for example, but you cannot order by taxonomy terms), but a problem with inserting the taxonomy shortcode shouldn't be the basis for your decision.

If you have imported the taxonomy activity-categorie I suspect this is the problem.

As a test can you please create a new test hierarchical taxonomy and assign it to your custom post type. Edit a few posts and assign some taxonomy terms that you make up for testing.

Now try to insert the taxonomy shortcode using the Fields and Views button and see if that works.

If it does then that suggests that the activity-categorie taxonomy has not been imported correctly.

Do you see any errors (JavaScript errors in the browser or PHP errors in the debug logs) when you try to insert the activity-categorie taxonomy shortcode?

#446897
Sans titre.png

Hi Nigel,

I had no errors while importing the "catégories d'activité".
But now, I have made custom fields with this.

I'm facing a new "issue" now.
I've created some new custom fields in my "activités" custom post type.
This CTP is a child of Organisme CTP (an organism could propose many activities).

I'm trying to insert the Title of the Organism CTP in a template for the "activités" to view the name of the organism (title) as the title of my activity, and all the fields of the activites selected (see screenshot).
But how to do? I find this fields nowhere.

Regards

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