Skip Navigation

[Resolved] Question about using Views to “filter” based on an attribute of a parent post

This thread is resolved. Here is a description of the problem and solution.

Problem:

There is a one-to-many relationship between Author and Essay; Author is the parent post of Essay. The Author CPT has a field named Person Type. Person Type has a couple possible values: student, faculty, and alumni.

I am trying to use a View to display a list of Essay posts whose Authors are alumni.Is this possible?

Solution:

Unfortunately, there isn't such a built-in feature within Views plugin, Views is using WordPress class WP_Query to query the posts, if you are querying "Essay" posts, Views can only filter the view by custom fields of "Essay" posts, it can not filter by fields of other post type, this is a limitation of WordPress.

Currently, I suggest you use [wpv-conditional] shortcode to check field "Person Type" value of related "Author" post, if the value is "alumni", then display the "Essay" post.

Relevant Documentation:

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 6 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by josephQ 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#922181

Hi there,

I have two custom post types: Author and Essay. There is a one-to-many relationship between Author and Essay; Author is the parent post of Essay. The Author CPT has a field named Person Type. Person Type has a couple possible values: student, faculty, and alumni.

I am trying to use a View to display a list of Essay posts whose Authors are alumni. Does this make sense? Is this possible?

My site is located at hidden link.

#922306

Hello,

Unfortunately, there isn't such a built-in feature within Views plugin, Views is using WordPress class WP_Query to query the posts, if you are querying "Essay" posts, Views can only filter the view by custom fields of "Essay" posts, it can not filter by fields of other post type, this is a limitation of WordPress.

If you agree, we can take it as a feature request, our developers will evaluate it.

Currently, I suggest you use [wpv-conditional] shortcode to check field "Person Type" value of related "Author" post, if the value is "alumni", then display the "Essay" post.

See our document:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#922651

Luo,

Thanks for your reply. While I'm a saddened to hear that Views is unable to perform this task, I'm not really surprised; this is a bit of a sophisticated request. Using conditionals is a good suggestion but won't work in this instance.

My plan is to create a shortcode and write a custom SQL query to retrieve the desired posts. What do you think? And is there a "Toolset Types" way that this task should be approached?

Thanks!

#922781

In my opinion, you can try the Views filter hook wpv_filter_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

for example:
when user submits the custom search form, you can use "wpv_filter_query" to trigger an PHP function, in this function, get the value of "Person Type":
hidden link

With the value "Person Type", then get the "Author" post IDs:
https://codex.wordpress.org/Class_Reference/WP_Query

With the the "Author" post IDs, get IDs of related "Essay" post
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Apply the "Essay" post IDs into Views query:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

But since it is not a built-in feature of Views plugin, the AJAX feature of custom search form won't work as expected.

#923032

Luo,

Thanks for yet another detailed and comprehensive response. I'm going to give my original approach (custom query) a shot first; I think it'll be easier. 🙂