Skip Navigation

[Resolved] Using the WordPress REST API to fetch a shortcode to display Toolset View

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

Problem:

Using the WordPress REST API to fetch a shortcode to display Toolset View.

The section at the top is a view displaying only 1 item, the selected coupon when the link "View Coupons" is clicked. However, when the coupon is on another page because the page reloads the current pagination selection is lost.

Solution:

Toolset Views does not support outputting [wpv_view] shortcode pagination into WordPress REST API result, the client resolved this issue by custom codes:

https://toolset.com/forums/topic/using-the-wordpress-rest-api-to-fetch-a-shortcode-to-display-toolset-view/#post-1714275

Relevant Documentation:

This support ticket is created 4 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 2 replies, has 2 voices.

Last updated by rotimiA 4 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1713313
Screen Shot 2020-07-20 at 7.18.41 PM.png

The section at the top is a view displaying only 1 item, the selected coupon when the link "View Coupons" is clicked. However, when the coupon is on another page because the page reloads the current pagination selection is lost.
I found two ways around this:
(1) Add Javascript to the link that saves the current page in the href of the "View Coupon" link
(2) Create a RestAPI route that returns the [wpv_view] shortcode. I could even load this route dynamically(ajax) so the page never refreshes
I have a few questions:
(1) Is there a better way to approach this problem that I have not found using built in toolset functionality
(2) Are there any considerations with the RestAPI route you can think of off the top of your head that I should consider
(3) Is using a view the best way to represent one custom posts content and custom fields. I know I could build the output code on the backend before returning it. Not only that even with this implementation, it seems I might be missing something since I am displaying a view with only one item showing at the top of the page.

#1713403

Hello,

Q1) Toolset Views does not support outputting [wpv_view] shortcode pagination into WordPress REST API result, so I don't think there is other better workaround than you provided above.

Q2) Toolset Views is using URL parameter "wpv_paged" for pagination setting, you can consider to capture this URL parameter.

Q3) You can consider Toolset Content template, display all post information(post content, custom fields, taxonomies) in the specific content template. see our document:
https://toolset.com/course-lesson/creating-templates-to-display-custom-posts/

#1714275

My issue is resolved now. Thank you!

The Rest API did work. I created a custom route on my WordPress installation by calling with add_action('rest_api_init', function() {}) then within the function I called the do_shortcode([wpv_view]) this output the view code and will be suitable for using with infinite scroll and the use case in my post. I had to clean the output since wordpress replaces quotes with " Thanks for your help.