Skip Navigation

[Assigned] Trying to develop view that joins two related custom post types

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 0 replies, has 1 voice.

Last updated by davidL-7 2 days, 14 hours ago.

Assisted by: Minesh.

Author
Posts
#2797963

Hi,

I’m attempting to develop a View that essentially needs to display, filter, and order by fields across two related post types. The app we’re developing with Toolset is replacing an older version built in ColdFusion. In the old app, the query is accomplished via an INNER JOIN between two related database tables.

In our WordPress/Toolset setup, here’s what we’ve got:

1. Custom Post Type “Aid Accounts”, with custom fields account-id, account-name, account-number, is-active (checkbox, 1 for active, 0 for not active)
2. Custom Post Type “Aid Yearly Totals” with custom fields account-id, aid-year, aid-amount
3. Relationship “Aid Accounts Aid Yearly Totals”, which relates one Aid Account post to many Aid Yearly Totals posts (in plain English, one Aid Account, will be assigned different totals for different years.
4. Function using wpv_filter_query (Not developed yet, this is where I'm getting stuck.)

I tried to follow some documentation I found at https://toolset.com/forums/topic/run-custom-sql-query-and-attach-or-pass-to-view/ and some others I followed from references in that thread, but haven’t been successful.

Based on the above, my understanding is that to pull off the equivalent of what would be an INNER JOIN query in regular SQL, I need to set up a view that queries one of the custom post types, with no Query Filter setting in the View UI. But not sure which post type it’s best to query (parent or child in the one-to-many relationshp), and how to write the function that will get me the data I need from both related custom post types. Here’s what I’ve got:

add_filter( 'wpv_filter_query', 'aid_account_data', 99, 3 );
function aid_account_data( $query_args, $view_settings, $views_id ) {
if ( $views_id == 31028 && !isset($_GET['wpv_filter_submit']) ) { // ID of the view
// Not sure how to write this part of the function.

}
return $query_args;
}

Practically speaking, here’s what I need to do with the View:

* Join Aid Accounts to related Aid Yearly Totals (joined on account-id). Relationships are already successfully established.
* Filter the view by Aid Yearly Totals aid-year (could be in querystring), and then only show active accounts (Aid Accounts is-active)
* Loop through the filtered posts and display:
* Aid Accounts account-number
* Aid Accounts account-name
* Aid Yearly Totals aid-amount
* Order by Aid Account account-name

Here's the URL to our page-in-progress: hidden link
But it's not filtering (both inactive and active accounts are showing, and they'll need to filter by year as well). I'm showing field values from Aid Accounts, but don't know how to get the related data for the amount from the related Aid Yearly Totals field.

Thanks for the support,
David

#2798020

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will reuqire to understand your strucgture first and for that I will require admin access details.

When you say:
- Join Aid Accounts to related Aid Yearly Totals
How exactly you want to display the output of view?

Is it like this:
- Year
-- Year Total
--- Aid Account

Can you please share bit more deetails about what is your exapected output and send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2798190

Christopher Amirian
Supporter

Languages: English (English )

Hi,

This is Christopher, thank you for login information I tested and I managed to login. Please wait for Minesh to check the details as he is off today.

He will answer back tomorrow.
Thanks.

#2798371

Thank you Christopher, I appreciate the update. Minesh, hope you're enjoying your time off!

#2798379

Hi Minesh and TS Support Team,

Just a quick note to let you know that I'm in the process of implementing page access controls, so you'll now need to log in (either from the WordPress side or from the public facing login screen on hidden link) to view hidden link (once you're logged in, you're an admin and should be able to see/do everything.

Thanks,
David

#2798694

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Lets try to fix the issues one by one.

As per our support policy - we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported with this ticket.

I've added the aid-year dropdown filter to your view:
=> hidden link

Can you please confirm it works as expected.
- hidden link

Now, you want to filter the view resuls with account active using the checkbox is-active? if yes:
- The thing is that you can filter the view results using (custom field, taxonomy) for the post type you set your view to query.

In your case you set your view to query post type "Aid Yearly Totals" but the custom field "Is Active" belongs to post type "Aid Account".

I'll split the ticket with the querstion of filtering the view results with "Is Active". I'll followup there with the following split ticket:
- https://toolset.com/forums/topic/split-trying-to-develop-view-that-joins-two-related-custom-post-types-filter-child-post-result-with-parent-custom-field/

#2798846

Hi Minesh,

Thanks for the reply and for your work! So the Aid Year dropdown filter you implemented is working, but the Is Active checkbox is not.

I can pick this up on the other thread you split this into, but in my mind it's all related, so I'll just make a note here for the benefit of others who read.

I actually was less concerned about the filtering by year, and more concerned about how to work with the equivalent of what would be an INNER JOIN in SQL. In this case, the view is querying the Aid Yearly Totals custom post type, and the year is a custom field belonging to that post, so that's all straight forward.

BUT, the Is Active field, and the name of the Account are both fields belonging to the Aid Accounts custom post type (which is related, on the one side of a one to many relationship). I think currently the Is Active checkbox you added is not working because the fields from Aid Accounts are not part of the query (as they would be in an INNER JOIN). Also, I need to order the results by the Account name.

Hope that makes sense. To restate the problem more simply:

- I have two related post types: Aid Accounts (one) related to Aid Yearly Totals (many).
- I need to query them (currently we're querying Aid Yearly Totals in the view) and do stuff with custom fields from both posts.
- I'm thinking we need to do that via a function using wpv_filter_query. I didn't use a query in the view itself, because everything I had read in the support docs indicated that when you use wpv_filter_query, you don't want a filter set in the view itself.

I'll leave this thread unresolved and look for your response here. Thanks!