Skip Navigation

[Resolved] Custom search on post relationship taxonomy

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by yusufS 1 year, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2454231

Tell us what you are trying to do?
Hi,

I'm trying to create a custom search on an archive page and I need to filter the results based on taxonomies and custom fields on different post relationships.

I have a custom post type A and I have a taxonomy "category" created for it. I also have a custom post type B and I have a taxonomy "counties" created for it. There is a many to many relationship between these custom post types and there's a multi checkbox field attached to this relationship.

What I want to do is to add a search to the archive page on custom post type A, filter them by "counties" and also by the custom field on the relationship. For some reason, the search won't work by these custom relationship fields/taxonomies. I tried using the "Post relationship or repeatable field groups owner" and also using the Taxonomies directly on the search fields but none of them work. What's the right way for me to create these search fields?

Thanks

Is there any documentation that you are following?
https://toolset.com/2020/03/helping-visitors-find-what-they-need-using-custom-searches/
https://toolset.com/lesson-placement/lesson-placements-1622935-1621213/#how-to-search-by-post-relationships

Is there a similar example that we can see?
-

What is the link to your site?
The website is not live yet

#2455123

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

In Toolset views, search filters can only be used for the custom fields and taxonomies, attached directly to the post type that is being shown through the view. The custom fields and taxonomies attached to the related parent/child posts or the intermediary post, can't be used.

If you could share the details of your requirements, I'll be in a better position to suggest some alternate structure to make this work.

regards,
Waqar

#2455255

Hi Waqar,

Thanks for letting me know. I have two custom post types: Camps and Venues. There's a many to many relationships on them. Camps may happen on different venues on different dates (multiple times) and there might be multiple camps on a venue at the same time.

Venues have a "counties" taxonomy on them that shows which county they are in. The relationship between Camps and Venues also have a multi checkbox "date" information. Venues also have an address field on them as well.

I was hoping to search the camps by a date filter (show me the camps that are available on this week) and also based on country (show me the camps that are happening around my county). If I can make this search work, I also want to list the venue addresses on a Map.

Is there a better way to create this relationship so that I can do this?

Thanks

#2456151

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

Based on what you've shared, you'll need 3 custom post types and not 2:

1. Custom post types:

a). Camps
b). Venues
c). Camping Events

As the name indicates, for each individual camp that is taking place at a certain time at a certain venue, you'll add a new post/record in this new post type 'Camping Events'.

The number of required post relationships will also increase:

2. Post relationships:

a). Camps -> Camping Events:
One to many, so that one 'camp' can have many 'camping events', but one 'camping event' can link to only one 'camp'.

b). Venues -> Camping Events
One to many, so that one 'venue' can have many 'camping events', but one 'camping event' can link to only one 'venue'.

After that, any general information that is about a particular 'camp', you can store it with its 'camp' post, in form of custom fields or custom taxonomies.

And any general information that is about a particular 'venue', you can store it with its 'venue' post, in form of custom fields or custom taxonomies.

Lastly, any specific information that will change with respect to each instance of a camp (for example date and time), will be saved with its 'camping event' post, in form of custom fields or custom taxonomies.

You'll also have to make sure that to be able to search these 'camping events' by certain custom fields or custom taxonomies, it is attached directly to this 'camping events' post type. You won't be able to search the 'camping events' posts, by the custom fields or custom taxonomies that are attached to 'camps' or 'venues' post types.

#2457533

Thanks Waqar. That would work but it would require me to duplicate a lot of data for the whole thing to work. Is there a way to automatically copy some data from one post to another through relationship?

#2458301

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

There are a couple of approaches that you can use to automate this copying/syncing of the data, between these post types:

1. Toolset offers a hook "toolset_connect_posts" that can be used to execute a custom function, every time two posts are connected:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

The downside of this approach is that this function will only execute at the time of relationship creation. So if the data at the source post is changed later, it won't be changed at the copied "Camping Events" post.

OR

2. Another option is to use the "save_post" hook from WordPress, which is executed, every time a post is created/updated:
https://developer.wordpress.org/reference/hooks/save_post/

Your custom function will perform these operations:

a). if the post being created is from the "Camping Events" post type, it will get the required data from the participating posts and will save it with the new "Camping Events" post.

And

b). if the post being edited is from the "Camps" or "Venues" post type, it will get the list of all the related "Camping Events" posts and then update the relevant data from the current source post to those copied "Camping Events" posts.

This way, the data will stay in sync, even after the relationship has been created.

I hope this helps and let me know if you have any follow-up questions.

For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2460197

Sorry for the late reply. I was able to fix the problem with the way you suggested.
Thank you

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