Navigation überspringen

[Gelöst] A View is behaving differently when it is invoked from within another View

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I have a View that is filtered by post relationship, based on a shortcode attribute. When I display the View on its own, the results show up as expected. However, if I nest the View inside another View, the results no longer appear.

Solution: In this case, some custom code using the wpv_filter_query API was being applied too broadly and affecting the results of the nested View. Some conditional logic is required here to apply the filter more selectively.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created vor 5 Jahren, 8 Monaten. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 10 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von alexG-4 vor 5 Jahren, 8 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1237390

I have a View associated with a Repeating Field Group of a post.

When I invoke it on its own, it works fine e.g.

[wpv-view name="grid-row-display" wpvrelatedto="488"]

In this case, 488 is the post ID of a post of type Asset.

In its current form, all it does is display a single row, consisting of all the (single) fields in the repeating field group.

However, the bigger picture which I am working towards is that I want to use the contents of the Repeating Field Group to define exactly how I display the fields in posts of a different post types.

For example, the Repeating Field Group in the Asset post with ID 488 will determine exactly what gets shown when I display the posts from the Lists custom post type.

I'm still trying to figure out how to do this, but my first step was to invoke the above View from within a View associated with Lists. Specifically:

[wpv-view name="grid-rows-for-assets" post_type="lists" ]

The loop of this view is:

  <wpv-loop>
          [wpv-post-link]<br>
          [wpv-view name="grid-row-display" wpvrelatedto="488"]
  </wpv-loop>

As you can see, it calls the exact same view as above, but in this context, it shows "No items found".

Here is a screenshot of a page that contains this HTML:

[wpv-view name="grid-row-display" wpvrelatedto="488"]
<hr>
[wpv-view name="grid-rows-for-assets" post_type="lists" ]

versteckter Link

I don't understand why the different context would change the behaviour of the View.

#1237422

It could have to do with the Query Filter settings or the checkbox "Don't include the current page in the results". Can you take screenshots of those settings in both Views? Be sure to open up any Query Filter configurations so I can check those.

#1237434

Thanks, Christian.

Grid Rows for Assets
versteckter Link

and
Grid Row Display
versteckter Link

Alex

#1237764

I don't see anything obvious in the Query Filter or other settings that would be causing this behavior. It's possible something is cached... can you try adding the cached="off" parameter to the nested View?

[wpv-view name="grid-row-display" wpvrelatedto="488" cached="off"]

If that doesn't work, please be sure to test for conflicts by disabling all non-Toolset plugins and disabling all custom code snippets in Toolset > Settings > Custom code.

Also, can you tell me about the post_type attribute here? What is its purpose, and how is it used?

[wpv-view name="grid-rows-for-assets" post_type="lists" ]
#1237806

Hi Christian

I disable all non-toolset plugins, added the cached attribute and removed most of the code snippets - but none of that made a difference.

I couldn't remove all the code snippets because I have code to adjust the Views filter to implement the post_type attribute.

The grid-rows-for-assets view applies to multiple post types, and I want to be able to invoke it by specifying which type to use each time.

I'll create the essence of the setup on a bare-bones site and report my findings shortly.

#1237827

OK - I tweaked my current setup instead, and it turns out that the problem IS my View filter code, which is:

add_filter( 'wpv_filter_query', 'kam_filter_by_post_type', 1000 , 3 );
function kam_filter_by_post_type( $query_args, $view_settings, $view_id ) {
  
  // get view's shortcode attributes
  global $WP_Views;
  $attributes = $WP_Views->view_shortcode_attributes;
  // check if a post type attribute is available in 1st item of $attributes
  if (!empty($attributes[0]['post_type'])) { 
    // set the attribute's value as post type
    $query_args['post_type'] = array($attributes[0]['post_type']);
   } else {
     // check if a post type attribute is available in 2nd item of $attributes
     if (!empty($attributes[1]['post_type'])) {
     // set the attribute's value as post type
     $query_args['post_type'] = array($attributes[1]['post_type']);
     }
  }
  return $query_args;
}

Do you think the problem is that $WP_Views cannot be used with nested Views, and that the nested View is actually being applied to Lists again?

If so, can you spot an easy fix for this?

#1237865

I don't see a problem using $WP_Views in a nested View, but it doesn't look like this filter code is limited in scope to any one specific View. What if you add a conditional that tests the $view_id parameter and applies logic based on which View is the "current" View? That means you can create an array of View IDs where you want to change the post_type of the Query, and for any other View IDs you can bypass that post_type manipulation.

#1237919

That worked, Christian - thanks!

It's a simpler approach than trying to figure out why it's not working in that particular case!

Case closed.

#1237942

Hi Christian

Now I've got this far, I can see exactly what I need to do for my "bigger picture", but can't see that Toolset allows it.

From within the loop of the inner View, I need to be able to access fields of the post in the enclosing View.

To recap....

This View, operates on List posts

[wpv-view name="grid-rows-for-assets" post_type="lists" ]

and the loop is

<wpv-loop>
        [wpv-post-link]<br>
        [wpv-view name="grid-row-display" wpvrelatedto="488"]
</wpv-loop>

Within the loop of the View "grid-row-display", I want to access fields of the List post currently being operated on in the View "grid-rows-for-assets", i.e. the post that would be shown by [wpv-post-link].

There is nothing I can see in the Fields & Views options that allows me to do this. I can see how to access fields of Related posts, of posts in a hierarchy, of the current Page, of posts with a specific ID etc - but I can't see any way to access the high-level View.

In my setup, this is the ONLY relationship available.

It doesn't seem such an unusual requirement, so maybe I'm missing something.

Might I have to do it by some PHP coding that operates on the $WP_Views structure? (I've taken a look at it, and can see that it copes with nested Views, but it's pretty daunting !)

#1237946

You can add any arbitrary shortcode attribute to the inner View shortcode, and set the value to be the current post ID from the grid-rows-for-assets View using the wpv-post-id shortcode. Then within the loop of the inner View, access that value using the wpv-attribute shortcode.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute

#1237950

Perfect!

Thanks, Christian - thought there had to be a way.

Alex