Skip Navigation

[Resolved] how to pass arguments to a view with block editor

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

Problem:
The user would like to pass arguments to a view used inside a Toolset View Block.

Solution:
As of now, it is not possible to pass arguments to views from a block in such similar way described in this article https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

Depending on your use case, you can use a classic block and then use the view shortcode inside it, or you will need to use a hybrid approach between blocks and views to perform that. This may also need an unassigned content template to hold the post context and then pass it to the view.

Check an example here https://toolset.com/forums/topic/how-to-pass-arguments-to-a-view-with-block-editor/#post-1572217

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#filters-by-custom-fields

0% of people find this useful.

This support ticket is created 4 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+01:00)

This topic contains 7 replies, has 3 voices.

Last updated by adrianM-6 4 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#1570541
Screenshot 2020-04-01 at 18.25.00.png
Screenshot 2020-04-01 at 18.20.25.png

Tell us what you are trying to do?
I'm trying to list posts with the same value for a particular custom field as the current post
Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes
Is there a similar example that we can see?
I've set up the query filter of the view to use custom field filter, as shown in uploaded image, I've used shortcode attribute 'birthdate'
when calling up the view in my template using block editor, how to i pass the argument to the view?
What is the link to your site?
hidden link

#1570761

Hello and thank you for contacting the Toolset support.

As of now, it is not possible to pass arguments to views from a block in such similar way described in this article https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

Depending on your use case, you can use a classic block and then use the view shortcode inside it, or you will need to use a hybrid approach between blocks and views to perform that. This may also need an unassigned content template to hold the post context and then pass it to the view.

Can you give more details on your use case and I'll try it locally to check what could be done with blocks and views?
Can you also paste your Toolset Debug information in the Debug information field?
- https://toolset.com/faq/provide-debug-information-faster-support/

#1571555

Hi Jamal,

thank you for your suggestions.
"use a hybrid approach between blocks and views to perform that. This may also need an unassigned content template to hold the post context and then pass it to the view."

I did that for another section on the same page. maybe i can do the same, but that was a parent template displaying child posts, any suggestion on how to pass the value of a custom field of the current post to the view as a filter?

my use case:
current post has custom fields, one of which is 'date-of-birth'
i want to list all others with the same 'date-of-birth'

Dave

#1572217

Hello Dave,

I am still not sure if you want to show the current post in a single post page or inside a loop(archive, view)! So I run a test locally and I was able to display the posts that have a similar date of birth in a single post page and in a view of posts.

Basically, the idea is to use a "Field and Text" block or a "Classic" block to pass the argument to the view with a shortcode.

I run my tests on regular posts types with a field group(Posts Birth Field Group) and a custom field date-of-birth.

1. I created a view(Same Date of Birth), I did not use blocks here, but I think that we can. This view will display the posts with similar date-of-birth. We will pass the date-of-birth using a shortcode attribute. Check this section https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#filters-by-custom-fields
Check the view query filter in this screenshot hidden link
The Date of birth field should be a number and equal to the value of the shortcode attribute "dateofbirth".

2. Then, I created an unassigned content template. This content template will hold the context(post) and pass its date-of-birth to the view created in (1).
The content template is created with blocks, and I used a "Fields and Text" block to use the view and pass the date-of-birth to it as an attribute. In the HTML mode of the block I added the following shortcodes:

[wpv-view name="same-date-of-birth" dateofbirth="[types field='date-of-birth' output='raw'][/types]"]

Note that the date of birth should be a timestamp and not a formatted date. You can choose that if you use the "Add Field or View" button on the "Fields and Text" block.

3. Use the unassigned content template(created in (2)) inside a block. In my test, I updated 3 posts from the "world" category with date-of-birth to verify.
- Either on a view. In my test, I created a page, and inserted a view block, created a new view that shows posts from the "world" category. Inside the loop, I used the unassigned content template.
- On a single post. In my test, I edited a post from the "world" category and used the unassigned template to show it.

I hope this helps. Let me know if you have any doubts.

#1576489

Hi Jamal,

Thank you for the suggestion. I am using a single post page for display. It worked out well.
With this approach, what if I wanted to pass 2 arguments?
not necessarily 2 arguments, but together with the method I wanted to also filter with post relationships.

Do you want me to mark this as resolved and open a new ticket?

thanks
Dave

#1576751

Hello Dave,

Well, for passing two arguments, you can do the same method/technique. But If you would like to filter by relationship, I would suggest marking this ticket as resolved and creating a new ticket. Please provide as many details about your relationship and on the page that you would like to create to get fast support.

Best regards,
Jamal

#1576871

My issue is resolved now. Thank you!

#1781813

Will this feature be supported in the block somewhen?