Skip Navigation

[Resolved] Query filter to shows items related to the post related to the current page

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 2 replies, has 2 voices.

Last updated by Minesh 6 months ago.

Assisted by: Minesh.

Author
Posts
#2698569
PXL_20240520_000611678~2.jpg
Screen Shot 2024-05-19 at 6.15.14 PM.png

I'm having trouble figuring out how to design a view (using the old method, not the blocks method) which accomplishes the following.

My site has a custom post type for WORKSHOPS like "Basic Training" and "Advanced Training."
My site has another custom post type for EVENTS like "March 24, 2024 class" and "April 30, 2024 class".

There's a one-to-many relationship between these two post types, in that there might be several EVENTS for each type of WORKSHOP.

Now, one last detail: The WORKSHOPS post type also has a one-to-one relationship with any WordPress page. The idea here is that I might have a landing page for each type of workshop where I talk about it, post videos, etc.

My challenge is that I'm trying to design a [wpv-view name="workshop-events"] shortcode that I can place on any page. This view should show all the events -> that are related to the workshop -> that is related to the page the shortcode appears on.

I can't figure out any way to get the Query filter to respond to this.

What does work is if I add a variable to my shortcode e.g. [wpv-view name="workshop-events" workshopID="44"] however the whole point behind relating workshops to an arbitrary wordpress page is so that on the page can just have a generic wpv-view shortcode that will pay attention to the one-to-one relationship between WORKSHOPS and WP-PAGES.

Any ideas, or is this level of abstraction in the relationships problematic?

#2698642
Screen Shot 2024-05-20 at 7.43.03 AM.png

For what it's worth, here's how I solved it for the time being. Obviously, it's a bit of a hack so if there's a more elegant solution I'd like to hear about it.

What we've done is programmed our own shortcode [WORKSHOPS-FOR-THIS-PAGE] which figures out the workshop ID related to the page, and then constructs a wpv-view shortcode with the workshop ID supplied as a variable.

/* The [WORKSHOPS-FOR-THIS-PAGE] shortcode will insert the workshop event schedule on any page. If it is placed
on the unique page which is RELATED to a particular Workshop, it will only show events related to that Workshop.
This function invoke the workshop-events Toolset view, dynamically supplying the ID of the workshop related
to this page (if any).
*/
function doShowPageWorkshops() {
global $post;
$html = '';
$page_id = get_the_ID();
// Get the related "Workshop" post using the Toolset API
$related_workshop_id = toolset_get_related_post($page_id, 'workshop-page');
if ($related_workshop_id) {
// Get the title for the post with $related_workshop_id and wrap it in <h2>
$related_workshop_title = get_the_title($related_workshop_id);
$html .= "<h2>$related_workshop_title</h2>";
// Invoke the [wpv-view] shortcode with the related workshop ID
$shortcode = '[wpv-view name="workshop-events" wid="' . $related_workshop_id . '"]';
$html .= do_shortcode($shortcode);
} else {
// Invoke the [wpv-view] shortcode without any related workshop
$html .= "<h2>All Workshops</h2>";
$shortcode = '[wpv-view name="workshop-events"]';
$html .= do_shortcode($shortcode);
}
return $html;
}

add_shortcode('WORKSHOPS-FOR-THIS-PAGE', 'doShowPageWorkshops');

#2698661

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I think shortcode you wrote is not needed.

What if you try to filter your view using shortcode attribute. So the post relationship filter you added - as you can see with the screenshot you shared:
- https://toolset.com/wp-content/uploads/2024/05/2698569-Screen_Shot_2024_05_19_at_6.15.14_PM.png

There is a filter option available "Post ID set by shortcode attribute" and you can give shortcode attribute name "wid" and save the query filter.

Then add your view as:

[wpv-view name="workshop-events" wid="[wpv-post-id item='workshop-page.parent']"]

More info:
- https://toolset.com/course-lesson/passing-arguments-to-views
- https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/