I created a view to display all children of a parent post type. Type B is a child of Type A. I want a filter that shows posts of Type B only if a custom field in Type B has the same value as a Custom field in Type A. I'm trying to use the Shortcode Attribute to pass the value dynamically but don't know that to do.
Hello and thank you for contacting the Toolset support.
The view you are trying to build needs to be filtered by two criteria:
- Have the same parent as the one passed in a shortcode argument.
- Have the same custom field as the one passed in a shortcode argument.
Then the view can be used by passing those values using the item arguement in shortcodes as follow:
[wpv-view name="the view name or slug" parent_argument="[wpv-post-id item="@{relationship-slug}.parent"]" custom_field_argument="[types field="custom-field-slug"]"]
{relationship-slug} needs to be updated according to your actual relation ship slug exp: "@studies.parent" if my relationship slug is "studies".
The views filters need to be:
- Filtered on the relationship to the post passed in the shortcode argument "parent_argument". Check this screenshot hidden link
- Filtered on the custom field passed in the shortcode argument "custom_field_argument". Check this screenshot hidden link
You can, of course, name "parent_argument" and "custom_field_argument" how do you like.
Thank you for your answer. I tried your solution (at least I think I set everything up right) and here is the result.
It's displaying all the posts from the child post type and not applying the custom field filter.
I'm going to try as much as possible to explain what I'm trying to achieve.
1. I have two post types called City and Office
- City is a bunch of cities
- Office is a bunch of offices
2. I have set up a one to many relationship between the two post types with City as parent and Office as child.
3. I have a custom field set up on both post types where I enter the same value of state name eg. Alaska. The custom field on the City post type has the slug "city_state". The custom field on the Office post type has the slug "location_state" (both are controlled by ACF). (I have also set up a custom field "viewsstate" that is displayed on both post types with the same values (controlled by Toolset Types)).
4. I'm creating a template for the City post type. I want to list all the related Office posts in the City post type template with views.
5. I want to set up a filter so that only Office posts with a "location_state" value that is equal to the value of the "city_state" value (I don't know if it can also be done with the "viewsstate" field). Basically if the City is located in Alabama, I want to show only offices located in Alabama. That is what I want to achieve with the two custom fields.
Please see the attached screenshot for my filter setup in views.
It doesn't filter the result when I use the view in a widget.
It doesn't filter when I use the shortcode below in my template.
From what I understand, you won't need to filter the view by the relationship or the related posts. You will only filter it by the custom field.
The ACF managed field needs to be detected and used by Toolset, for that you can detect it in Toolset->Custom Fields->Control custom fields.
If you still need assistance with this view, please allow me temporary access to your backend and I'll try to check it further. I'll try with the Alaska example. If you agree, your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.