Skip Navigation

[Resolved] A code that works on it’s own does not display when looped in a view

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

Problem:

Display parent post information with a post view.

Solution:

In a child post, you can display parent post information with "item" attribute, see our document:

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created 3 years, 7 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.

Our next available supporter will start replying to tickets in about 2.10 hours from now. Thank you for your understanding.

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 12 replies, has 2 voices.

Last updated by Luo Yang 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1782833
Screen Shot 2020-09-20 at 8.10.21 AM.png

Tell us what you are trying to do?

Hi..:-)..

I have code based on this ticket.. https://toolset.com/forums/topic/display-field-values-from-related-grand-parent-post/#post-1782239

you will see that the code that's used there is working well when displaying value on a page..

[wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"]

and here's the code from the content template

[types field="id" item="@brand-distributor-eyewear-brand.parent" output="raw"][/types]

It's being used to display values here and you can view it without logging in as there's no condition applied..

hidden link

So you can see the distributor ID as 1234 which is the result of the above code.

I need to display the same code but with a condition so I used a view to display the code.. the condition would be based on the result of a short-code.. this short-code is yet to be created.. but first I'd like to check if the working code can be displayed in a loop

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=other][wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

So you see I am using the same code as above.. but now in a loop and I am displaying it in a content template like this

[wpv-view name="related-brand-distributor-eyewear-collection-brand-distributor-id"]

So it's working fine when I am not using a loop but not when I use it in a loop.

But I need it to work in a loop so that I can create and apply a condition.. why does it not work the same way in a loop?

In the view I've selected 'Eyewear Brands' as the post types to display and added a filter
Select posts in a Eyewear Brands To Eyewear Collections relationship that are related to the Post where this View is shown.

Screenshot attached..
https://toolset.com/wp-content/uploads/tmp/Screen_Shot_2020_09_20_at_8.10.21_AM.png

Please do advise on how I can achieve this so that I can move to the next step of applying a condition to the content of the loop..

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1784037

Hello,

How do you setup the post types and relationships?
Please provide your website credentials in below private message box, also point out the problem page URL and view URL, I need to check and test it in a live website.

#1784123

Hi,

I think I forgot to add the links for the view that I had created

The link below is for the view related-brand-distributor-eyewear-collection-brand-distributor-id where I am trying to use the code in a loop.

hidden link

And here's the content template that I am using as brand-distributor-id-for-eyewear-collection

hidden link

Regards,
Alim

#1785005

Thanks for the details, I can login your website, and have done below modifications in your website:
Edit the post view "Related Brand Distributor Eyewear Collection Brand Distributor ID"
hidden link

In section "Loop Editor", add the shortcode inside view's loop directly:
id: [types field="id" item="@brand-distributor-eyewear-brand.parent" output="raw"][/types]

Test it in front-end:
hidden link

I can see it works fine

Can you confirm it?

For the question you mentioned above:
why does it not work the same way in a loop?

You are using "[wpv-item index=other]" in section "Loop Editor", in your case, you don't need it, you can remove it and test again.

More help:
hidden link

#1785033

Hi Luo

Thanks for the update..:-)..

I tried to remove the [wpv-item index=other] and even though your content and my additional content ie ,test: is now visible.. the output of [wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"] is still not visible

This is what is in between the loop code

id: [types field="id" item="@brand-distributor-eyewear-brand.parent" output="raw"][/types],test:[wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"]

You can check the result here.. hidden link

I am also surprised and curious to know how you managed to get the grand parent value of the brand distributor id just by using the types shortcode.. I am trying to do the same thing but with a 'wpv-post-body' with the content-template that holds the types shortcode..

And wouldn't I need [wpv-item index=other] if I have multiple values (multiple brand-distributors)? Coz I have multiple brand distributors actually.. Please do advise..

Regards,
Alim

#1785131

Yes, since it is within view's loop, current post is a single "Eyewear Brands", so you can access the parent "Brand Owners" post with attribute "item" directly.

I have done below modifications in your website:
1) Create a content template "brand-owners-information":
hidden link
with below shortcode:
[types field="id" output="raw"][/types]

2) Edit post view "Related Brand Distributor Eyewear Collection Brand Distributor ID"
hidden link
Modify your codes from:
test:[wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"]

To:
test:[wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-owners-information"]

Test it in front-end:
hidden link
I can see it works fine, please check if it is what you want, thanks

#1785273

This is absolutely brilliant!!

This is new learning for me.. but please do confirm if I am getting it right..

If I have to get the grand-parent field in a shortcode I have to use this code on eyewear collection (grand-child)

[wpv-post-body item="@eyewear-brand-eyewear-collection.parent" view_template="brand-distributor-id-for-eyewear-collections"]

And in the brand-distributor-id-for-eyewear-collections content template (which is not assigned to anything) I need to have this

[types field="id" item="@brand-distributor-eyewear-brand.parent" output="raw"][/types]

This is what I had been shown by Minesh and some documentation.. please do confirm if this is correct..

Now what you are telling me is that if I were to use a loop in a VIEW and use the view as a [wpv-view name="abcd"]
then in the view if it is the CHILD that has a query connecting it to the GRANDCHILD, then I can get the GRANDPARENT just by using [types item="@grandParent-child.parent"] [/types]

I've tried it and it seems to work.. and so the whole 'wpv-post-body' exercise seems unnecessary.. .

Please do advise if my above understanding is correct.. it will make my life a lot easier..:-)..

Once you confirm I'll close this ticket..

And then go to the next part for this quest.. I don't want to ask the next question in this ticket so that we can keep things structured...

Is there a way I can ensure that the next ticket I open is assigned to you? It would help.. please do let me know..

Have a nice day..:-)..

Regards,
Alim

#1785289

Yes, you are right, the whole 'wpv-post-body' shortcode is unnecessary, and actually, since you are using one-to-many relationship,
One "Eyewear Brands" post can connect with only one "Brand Owners" post
so in a single "Eyewear Brands" post, you can access it's parent "Brand Owners" post information with "item" attribute directly, so the post view "Related Brand Distributor Eyewear Collection Brand Distributor ID" is unnecessary too.

See my above answer:
https://toolset.com/forums/topic/a-code-that-works-on-its-own-does-not-display-when-looped-in-a-view-2/#post-1785005

#1785419

Thanks for sharing this information Luo... it will really help me going ahead..

One "Eyewear Brands" post can connect with only one "Brand Owners" post
so in a single "Eyewear Brands" post, you can access it's parent "Brand Owners" post information with "item" attribute directly, so the post view "Related Brand Distributor Eyewear Collection Brand Distributor ID" is unnecessary too.

I will explain in my next question why I am using this post view and I would appreciate if you could help me there too..

Have a nice day..:-)..

#1785421

My issue is resolved now. Thank you!

#1785583
#1785599

Here's the link to the new ticket if you are able to assist me..

https://toolset.com/forums/topic/not-able-to-display-multiple-post-values-when-running-a-loop/

#1786889

I was off, our collogues have taken your new ticket. thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.