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.
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:
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.
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.
New threads created by Minesh and linked to this one are listed below:
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 🙂