I am trying to:
How to Filter a View of 1 CPT based on information stored in related CPT 2.
I already Nested a view inside a view but want to know if there is a way to create a filtering option on the frontend to filter the View based on the 3 values of a related CPT [example: Approved, rejected, pending].
This topic is basically an extend from my previous topic:
https://toolset.com/forums/topic/related-posts-dosent-give-me-the-view-im-going-after/
Hello,
The Views plugin is using WordPress Class WP_Query the posts, so in a post view:
- Query posts of CPT-1
You can not filter the results by custom fields of CPT-1.
If you filter the results by custom fields of other post type "CPT-2", it will conduct the problem:
No item found
Since there isn't custom fields [example: Approved, rejected, pending] in CPT-1.
See WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
There is a feature within Views plugin:
Filter the results by post type relationship, but it will use related post title as dropdown selector, see our document:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/how-to-filter-posts-by-their-ancestors/
How to filter by related posts
I see your colleague Christian said the same thing but he mentioned that there is a way to achieve filtering by custom field and not title by using the API cred_save_data.
See his last comment:
https://toolset.com/forums/topic/related-posts-dosent-give-me-the-view-im-going-after/
Is there a more detailed way how to achieve this?
I assume we are talking about the post:
https://toolset.com/forums/topic/related-posts-dosent-give-me-the-view-im-going-after/#post-1227278
That suggestion is these:
- Register custom fields [Approved, rejected, pending] to both post types( CPT 1 and CPT 2)
So you will need to setup the duplicated fields in posts of both post types
- Use custom PHP codes to sync those duplicated fields value in posts of both post types
in that thread, it is using Toolset form to create/edit post, so Christian mentioned the action hook cred_save_data
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
But according to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/
You can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Our developers will evaluate it.
If you insist on custom codes to sync the custom field values, here are my suggestions:
1) When user create/update a CPT 2 post, use WordPress action hook "save_post" to trigger a PHP function
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
2) in this PHP function
- Get custom field [Approved, rejected, pending] value of current CPT 2 post
https://developer.wordpress.org/reference/functions/get_post_meta/
- Get related CPT 1 post ID:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
- Update custom field [Approved, rejected, pending] value of the related CPT 1 post as CPT 2 post:
https://developer.wordpress.org/reference/functions/update_post_meta/