Skip Navigation

[Resolved] Filter by post relation

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.

This topic contains 18 replies, has 3 voices.

Last updated by thomasS-11 6 years, 10 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#269343

Hi.

I finally set up my Many to Many relationship with 2 post types 1: Product - 2: Application. I have a Intermediary post type called Relations but lets ignore that for now as i think we dont need this - im not sure though thats why i mentioned it.

Anyway.. I have this relation between both post types.
I have now created a product page to list all "Products" .. i want to have a front-end filter that allows the user to filter by relation. Is this possible? So instead of creating a Taxonomy for products, i can just list the Applications in a selectbox, and when Application is chosen from selectbox it should show only the Products that are related to the selected Application - is this even possible?

I know i can make a Taxonomy for the Products named Application-category, and just filter by that, but i dont want to make too confusing backend for the end-user when im going to deliver the project, therefore i think when relation is selected they shouldnt have to select the same again but in Taxonomy.

I hope my question is clear enough.

Thanks.

#269629

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

You will need query the child post type "relationship", and display it's parent post "Products", see below test site:
hidden link
user/pass: xgren/111111

filter:
Select posts that are children of the Post with ID set by the URL parameter wpv-pr-child-of.
eg. hidden link

See the result:
hidden link

#270380

Very good.
How do i output it? - i dont want to use the same view for it since i am going to have 2 filters: 1) category - 2) relation.

How do i output both filters? it seems i cant import views in views? i thought "Combined output" was for this purpose, but i can't seem to get it to work.

I want it to be like this:
First filter - Second filter (50% - 50%)
Results/Output (100%)
Dont mind the percentage, its just to make it easier to understand what i am trying to achieve.

I already read:
https://toolset.com/documentation/user-guides/front-page-filters/
But i cant find the answer here?

Thanks.

#271230

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Sorry for the delay answer,

It is not possible within Views, since the Taxonomy “category?? is being registered to post type "Product", and above view is querying the intermediary post type "Relations“,
it is different post type, you can not I don't think there is a workaround within Views, you can check out our certified partners for it:
https://toolset.com/consultant/

#271275

I can't make a list depending on 2 filters?
1) category
2) relation

its like if you are to make a Real Estate website (like you do alot in your demos) and i want to find a room based on size and property?

#271507

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

As the demo site I mentioned above:
hidden link
it is a demo site of "Bootstrap Real Estate"
The post type "Property" is child post type of post types "State" and "City", the "size" is a custom field of "Property", and the view is querying the post type "Property":
hidden link

In your case, "Relations" is the child post type of post types "Product" and "Application", Taxonomy “category?? is being registered to post type "Product", it is different with above demo site,

There are two options you can try:
1) setup Taxonomy “category?? to post type "Relations"
2) use custom Views filter hook wpv_filter_query_post_process to check if current Relations post have a parent Product with term set be URL parameter
wpv_filter_query_post_process:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query_post_process/

#271533

If i use custom field instead of category, can i filter on 2 criterias then?

#271612

i really need to be able to filter between 2 criterias 🙁 .. also, the relations is occuring double sometimes because of multiple relations (many to many) and since its querying relations and not posts (which i still have hard time understanding) i cant control the dublettes..

I want to have:
Post type A (Product) many to many relation with Post type B (Product Application) by Post type C (intermediary post type) and Category (Either by taxonomy, custom field, or maybe a Post type D, its not important to me how its created as long as it works)..

I want a product list (not by relations, otherwise products will appear multiple times if has multiple relations) where i can filter by Product Application (Post type B) and Category (Taxonomy, Post type or Custom field).

Is this not possible somehow?

Thank you and sorry for my trouble..

#271672

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

As I mentioned above:
https://toolset.com/forums/topic/filter-by-post-relation/#post-271507
There are two options you can try:
1) setup Taxonomy “category?? to post type "Relations"
2) use custom Views filter hook wpv_filter_query_post_process to check if current Relations post have a parent Product with term set be URL parameter
wpv_filter_query_post_process:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query_post_process/

The first option need to modify the post Taxonomy, but without custom PHP codes
The second option need to create custom PHP codes, and you can use it to remove the duplicate items.
and I dont think there is other workaround within Views,
Also you can Check out our certified partners for it:
https://toolset.com/consultant/

#272284

1) so you want actually to go into relations and add category to relations? sorry to say it but that is a VERY unpractital idea.. and it makes no sense what so ever to go to relations, look at relations and add category for it.. seriously, im starting to have very low thoughts on WPViews.. too many "simple" things i expect from views cannot be acheived, and if they can its very illogical really.. PLEASE FORWARD THIS TO YOUR DEV TEAM - because this is really a thing i want to "complain" about.. the fact that you get dublettes on a query showing the relation, and that i have to categorize the relations .. its really stupid..
This is not a personal attack Luoy, its my "Feedback" for your dev team and is in no way ment for you as a person.

2) Im not experienced in PHP, i know nothing about hooks and stuff, and Views claims to be codefree, so this solution is out of the question. Please keep this in mind next time you support a client.

Regarding WP-Toolset consultants, it is also of no use.. im not learning by paying others to do my work.. therefore i am only using the supportforum, and not any Consultants, but thanks for the suggestion.

I am very satisfied with your support, but can you check if someone else knows the secret solution for my issue? Thanks.

#272303

I found a solution im going to work with, as above is somewhat hopeless to me..
I want to ask a final question.

I went with Posts2Posts plugin from WordPress, very good plugin and easy to setup.
Now, i have connected my posts to eachother using the Meta Box that allows (multiple) connections.

I want to control it in views, but nothing pops up as a custom field..

Most other plugins running in my website, i can actually use though WP-Views, but not this one..
My question is, is it possible to use own fields (not Types) with Views? and if yes, how?

Please look at Posts2Posts hidden link so you can give a more percise answer to what i am looking for..

Please consider using same connection type for parent/child relation with no many-to-many, as it is way more simple and easy to use..

I have a feeling that IF i can get Posts2Posts field/connection in WP-views, i am able to acheive the above by using Taxonomy to filter "Categories" and Post2Post connections to filter for "Applications".

I know you are supporting alot of people, but as i am WAY behind with this project, i ask you for a fast answer.

Thank you.

#272490

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the feedback, The
1) OK, I put it into our to-do list as a feature request,
2) It is an option for your case, it need custom PHP codes. if you need more help for it, you can describe detail steps to duplicate same problem, what slugs are you using for each post type, what slug are you using for the Taxonomy category, I need test and debug the PHP codes in my localhost
3) for Posts2Posts plugin, it also need custom PHP codes, please check the URL you mentioned above:
hidden link
Displaying connected pages/posts

// Find connected pages
$connected = new WP_Query( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object(),
  'nopaging' => true,
) );

There you can apply Posts2Posts parameters into views query, with Views filter hook
wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.

https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

I am not familiar with Posts2Posts plugin, same as 2) I need detail steps to duplicate same problem and test and debug in my localhost.

#272975

1) Thank you very much
2) - i created these post types and category (No 3rd party plugin)
------------
a) Product (Post type)
b) Application (Post type)
c) Relations (intermediary Post type)
d) Product category (Taxonomy)

I connected Products and Applications through Relations.
I added Product category to Product.
All slugs are the names of the post types and taxonomies.

I want to display all my products, and be able to filter from application, and product category.
--------------

3) So far i have almost made it using Post2Posts plugin, but i just need to be able to get the field though views, and im pretty sure it will work. i can get PixGridder's (plugin) field in filter, and many other plugins. I just cant get it to show Post2Posts in views.. and i dont know how to render PHP in views, obviously i cant do it in the HTML field.
I did check the provided URL, and i know how to display the connections in the php template files, thats not the problem - i want to be able to use the relations in Views.
Im sorry but im not sure how to handle this part you posted in earlier comment:
"There you can apply Posts2Posts parameters into views query, with Views filter hook
wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class." - what exactly must i do? put the code in my functions.php and use views filter hook?

Thank you for your help and understanding, and sorry for taking so much time.

#273091

Waqas
Supporter

Languages: English (English )

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

This is Waqas here, on behalf of Mr. Luo. He's on vacation and will join back on Monday.

He will follow up with you, as he returns. Thank you for your cooperation and patience.

#273216

Hi Waqas.

Thanks alot for the update.

I wish him a good vacation.

Have a nice day.