Skip Navigation

[Resolved] How to limit Views query to page/post in which View is displayed?

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jerry Hudgins 7 years ago.

Assisted by: Nigel.

Author
Posts
#591011

I'm a new Toolset and WP user, trying to implement a common mechanism familiar to me from Drupal Views. I have a need to apply a View to only the page or post on which that View is displayed, extracting and formatting data for display in a widget on that page. In this particular case, the featured image was the data to be displayed.

I expected that there might be a filter option to permit this, and tried using a query filter to include only posts whose post ID was [wpv-post-id], but that failed - no records were returned by the query. I then tried the solution which I later found described here:

https://toolset.com/forums/topic/views-loop-showing-all-posts-instead-of-current-post/

in which a text widget included the View shortcode using the ids="[wpv-post-id] attribute to filter the returned IDs. However, that didn't work for me, nor did ids="$current_page". In both of those cases, all records were returned without filtering.

I eventually found a workaround for this particular case: I limited the query to 1 record, and specified the ID of the current record in the token which displayed the image in the Loop Output Editor:

[wpv-post-featured-image size="full" id="$current_page"]

However, that's clearly a suboptimal approach for more complex situations. What's the right way to do this in Toolset?

Perhaps there should be a query filter option specific to this situation, or perhaps the "Don't include current page in query result" option should be expanded to a radio button: include only the current page/exclude the current page/no filtering based on current page. What are your thoughts on this?

#591175

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2017-11-20 at 10.46.15.png

Hi Jerry

I'm not sure what the use case is where you would want to create a View that returned just the post where the View was inserted, because whatever content you include in the View you could presumably include in the post or template for the post itself?

Anyway, here's how to do it.

Create your View and a add a filter to only include posts whose ID is specified by a View shortcode attribute, as shown in the screenshot.

Now, where you insert the View (in a post, or in a template for posts) add an attribute that corresponds to that expected by your filter which uses the wpv-post-id shortcode to populate the ID, like so:

[wpv-view name="post-where-view-is-shown" ids="[wpv-post-id]"]

If you use the wpv-post-id shortcode on a single post, or in a template for a post, it will output the ID of the current post.

#591319

Thanks for the reply, Nigel.

I think that you missed the fact that the View in question in being used to populate a widget, and the widget is expected to show data relevant to the post/page on which it is shown. That's why it needs to filter for the currently-displayed content. Please also note that I tried the solution that you suggested (see the issue linked in my post), as well as a variation on it; neither worked. I expected that it would, and it did in the case described there, but it did not work for me. That issue is a year or two old, and perhaps subsequent software changes have affected the solution given there.

#591574

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jerry

On single post screens or pages the context for the widgets is the currently displayed post or page, so if you add Views shortcodes directly to a Custom HTML widget, for example, they will output content for the displayed post.

So a View to return the current post is redundant in that scenario.

You can either design the content of the widget directly in the widget itself, manually inserting Views shortcodes as required, or you could create a Content Template which you do not assign to any post type which you populate as required, and then insert this template into a Custom HTML widget.

So, I made a Content Template called Widget Template, for example, and inserted it into a widget using the shortcode [wpv-post-body view_template="widget-template"].

This doesn't work on archives, but from your question you are aiming to do this on single posts or pages.

To check if it is working try adding a Custom HTML widget and inserting [wpv-post-title], then visit a single post or page on the front end, and you should see the title of that page displayed in the widget.

#591793

I understand that in this limited situation, one could simply display a field from the current content in a widget by embedding the appropriate wpv-* tag. That one could make this display more elaborate and display multiple fields using a content template outside of a View is something I didn't realize, and I appreciate the tip.

However, it's not difficult to construct a scenario where you really *do* need to embed a View, not just a field or template, into a widget and have that View operate on the contents of the display page/post. A simple one is a slideshow of images attached to a custom field of the page. It occurs to me that I may be making an incorrect assumption here, based on my Drupal experience - is it possible for Toolset Views to iterate over the contents of a repeatable field within a single page, rather than over multiple pages? As a specific case, can a View create a slideshow from a repeating image field attached to a given page? If not, I concede that my line of inquiry here may be pointless.

#591947

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jerry

If you add a View using a Custom HTML widget so that you can add shortcode attributes, then you can insert the View with an attribute of ids="[wpv-post-id]" and then in the View itself add a Query Filter for post id that is set by the same shortcode attribute. I just tested that and it worked as expected on a page where I was viewing a single post.

Regarding making a slider from repeating image fields, yes, it can be done, but you don't need a View (which iterates over posts) for it. You can use the wpv-for-each shortcode in a Content Template to iterate over the fields (https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each).

We are preparing a knowledge base section for our documentation which is not ready yet, but I produced a draft showing how to create an image slider from repeating image fields which you can see here: hidden link

#592327

Ah, I see that I had misunderstood the Views query filter "Posts with IDs set by the View shortcode attribute" setting, and while I'd tried the Views shortcode with ids attribute in a text widget, I hadn't properly set the View filter to recognize it. When I do, it works as expected for me, too. Thanks.

Regarding the repeating fields slider: thanks for the preliminary notes on that. Makes sense that I'd have to use the wpv-for-each to iterate over the field in this environment. I imagine that I can put together something using your Module Manager to make it fairly simple to drop a repeating field-based slider into a new application.

I appreciate your patience as I figure out how to use Toolset properly.

The forum ‘Types Community Support’ is closed to new topics and replies.