Skip Navigation

[Resolved] I need help with Filtering by Custom Field

This support ticket is created 3 years, 9 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.

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: Africa/Casablanca (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#1703353

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.

#1703527

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

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.

More about the item argument here https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

I hope this answers your question. Let me know if you still need assistance with this.

#1703663
shortcode render.png
filter.png

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.

[wpv-view name="popular-offices-in-state" wpvrelatedto="[wpv-post-id item="@city2office.parent"]" customfieldargument="[types field="viewsstate"]"]

And the shortcode displays the list of offices with the last part of the shortcode showing on the frontend as in the second attachment.

I hope this clarifies what I'm trying to do.

#1703763

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

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.