Skip Navigation

[Resolved] Use a View to display data from the current page only

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

Problem:
Use a View to display data from the current page only

Solution:
You can use the attribute item="$current_page" in order to display the data of current page.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/use-a-view-to-display-data-from-the-current-page-only/#post-1161349

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-found

This support ticket is created 5 years, 11 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
- 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 4 replies, has 3 voices.

Last updated by Ben 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1160941

Ben

I am aware that it is not best practise to try and use a View to just display Custom Fields from the current page only, but best to insert the shortcodes into a page template or directly onto the page itself. However for me, this approach is becoming more and more difficult as I am dealing with a large quantity of Custom Fields, and the ability to be able to edit them from one place is invaluable. I also like using Views as if I need to change something in the View at a later date I can do that very easily.

I know there is no option built into the Views GUI to query the current post only, but I appear to have found a workaround. I select Content Selection to be whatever Custom Post Type I need the information from, untick "Don't include current page in query result" and leave the Query Filter blank.

If I take these settings and then leave no values between the <wpv-loop> tags, I am then able to place shortcodes outside of these tags and use item='$current_page' to display Custom Fields from wherever the View is displayed.

This seems to work fine mostly, but I have found out that when there are no results from the query, and therefore nothing displayed inside <wpv-loop></wpv-loop>, this also seems to affect the Custom Fields inserted within the view but outside of <wpv-loop> tags. These Custom Fields will not show.

Why is this? And can I stop this happening?

#1161349

Hello,

I suggest you try with a content template "current-post-ct", put all custom field shortcodes of current page into this content template, and display the content template with attribute item='$current_page', like this:

[wpv-post-body view_template="None" item="$current_page"]

And you can put above shortcode outside [wpv-items-found] ... [/wpv-items-found], so even there isn't any result found, the content template will still be rendered.

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-found
Meta HTML wrapped in [wpv-items-found][/wpv-items-found] will be output if there are any posts | taxonomies | users returned by the View.

#1162070

Ben

Thank you for your reply Luo Yang. That works very well!

Just a question out of interest regarding my original post.
Do you know why that, in a View when there are no results from the query and therefore nothing displayed inside <wpv-loop></wpv-loop>, this also seems to affect the Custom Fields inserted within the view but outside of <wpv-loop> tags. These Custom Fields will not show in this situation.

#1162725

Minesh
Supporter

Languages: English (English )

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

Luo is on vacation. This is Minesh here and I'll take care of this ticket and try to help you further. Hope this is OK.

Glad to know that your issue is resolved. I'm splitting the ticket to guide you in the right direction for your add-on question. Please feel free to close this ticket 🙂

#1163027

Ben

Thank you Minesh.