I want to implement 2 post types
CPT person
CPT skill
I want to show a view of skills in the content template for person. The skills that need to be shown are depending on 2 variables:
1. age of the person
2. gender of the person.
Example:
I have a skill 'agility'. This skill should only be shown for MALES between the age of 8 and 12 and for FEMALE only between 6-14 years of age.
So the filter should be:
When the person is MALE AND the AGE is between 8-12 then show the skill agility in a view skills
When the person is FEMALE AND the AGE is between 6-14 then show the skill agility in a view skills
I am not sure how the configure this, so if you have some thoughts on this, please share!
Hello and thank you for contacting the Toolset support.
I assume that a person can have multiple skills, right?
If that's the case, this needs a many-to-many relationship between CPT person and CPT Skill. A many-to-many relationship generates a hidden CPT that assures the link. We call it an intermediary post. Check this article https://toolset.com/course-lesson/many-to-many-post-relationships/
Views queries cannot search with other custom post types' custom fields. It's not possible out-of-the-box to query a child post using the parent's or the intermediary posts custom fields and vice-versa.
This being said, I can see multiple ways of achieving what you want, some of them have limitations:
- You can put a condition inside the skill view loop that checks for the parent's post custom field. This has the limitation of having the wrong results count.
- You can put a condition on the person content template and conditionally display a view that will look for the skill "agility".
- You can add custom code that hooks into the view's query and change the results.
- You can hold the date or year of birth of the person in the intermediary post in a many-to-many relationship, and query-based on age. This may also need custom code.
Honestly, I need to know more about your project to give accurate advice. In the meantime, please check this article on how to display related posts https://toolset.com/course-lesson/displaying-related-posts/
There is no direct need for a relationship. I only want to show a post title if the age of the person is between the min-max age of the skill. This is my focus, how to check this when rendering the view. Should I use custom fields on the post 'skill' like max_age and min_age and test on those fields?
I would say to put the title inside a conditional block. The conditional block will check the person's age and display the title if the user matches the conditions.
I wonder why are you talking about a view? A view is meant to query the database and display a list of posts. If there is no relationship between person and skill, why would you like to display a view of skills inside the person's page?
Perhaps an example can explain what I am looking for.
CPT person -> John Doe, John is 14 years of age
CPT skill:
-> agility, min_age = 9, max_age is 12
-> speed, min_age = 6, max_age = 12
-> strength, min_age = 10, max_age=18
-> stability, min age = 12, max_age = 16
When I open content template for John Doe, I want to see the following skills in a view or whatever
- strength (because John's age (14) is between min_age 10 and max_age 18)
- stability (because John's age (14) is between min_age 12and max_age 16)
Thank you for this example, it really helps to understand what you want to build, but it's also tricky and I need to know how you save or intend to save the data. Why? Because I assume the following:
- A skill age requirements won't change over time.
- A person's age will change over time.
And because dates or times are saved, in Toolset, as a timestamp. The timestamp is the number of seconds since the Unix Epoch (January 1, 1970, 00:00:00 GMT)
hidden link
To better assist you with this case, I wonder if you would like to prepare an example in one of our test sites. You can log in to this test site using the following URL hidden link
Please create both CPTs, and prepare the example that you shared above. I'll check how the data is set up and I'll prepare an example for you.
There is no need to save the data, the skills just have to be shown at the moment the person is shown. So when the age changes of the person, another set of skills will be shown.
I prepared an example in the site you sent me based on my example data in this thread
The way you saved the age of a person will make it not change over time. But it makes creating the view more simple. The trick is to pass the age to the view using a shortcode attribute. You can read more about it here https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes
The view has a query filter like this hidden link
Then we pass the person's age in a shortcode attribute like this:
[wpv-view name="persons-skills" age="[types field='person_age'][/types]"]
Check the results on this screenshot hidden link
hidden link
Regarding the age: I know, this was to keep it simple and for demonstrating purposes.
I play around with your example.
Ok. I'll set this ticket as waiting for your feedback, which should keep it open for 3 weeks.
Let me know if you need further assistance.
My issue is resolved now. Thank you!