Skip Navigation

[Resolved] Creating a View

This support ticket is created 3 years, 10 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
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: Africa/Casablanca (GMT+00:00)

This topic contains 16 replies, has 2 voices.

Last updated by filipeC 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#1657123

I am trying to create a simple unordered list View to put in an Elementor template.

It should look like this:
- 6 objects (14 lb. each)
- 4 things
- 12 pieces (17 oz. each)

It's built from four fields:
Number of things: 6, 4, 12
Type of things: 'objects', 'things', 'pieces'
Weight: 14, [blank], 17
Unit of weight: 'lb.', [blank], 'oz.'

I created a View and built a loop using the Block editor, using the Texts and Fields block. But currently it looks like this:

- 6 objects (14 lb. each)

- 4 things (each)

- 12 pieces (17 oz. each)

I have two questions:

1. How do I create a View that lists the four fields, but makes the appearance of the parentheses and the word 'each' dependent on whether there is any data in the 'weight' and 'unit of weight' fields? I have both the Blocks and Legacy editors enabled.

2. How do I control the spacing between the list items?

#1657147

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

For both questions, you can use the legacy editor and create conditional shortcodes on the value of each field to display/hide the "(each)".

In Blocks, you can use the "Fields and Text" block or the "Classic" block, which will let you use the legacy shortcodes inside blocks.

For the second question, in blocks, I believe the only possible way will be to use the "Fields and Text" block or the "Classic" block. But I'll need to take a closer look to suggest the most accurate solution. If you would agree to allow me temporary access to your website, your next reply will be private to let you share credentials safely.
** Make a database backup before sharing credentials. **

#1658873
<!-- wp:toolset-blocks/fields-and-text -->
<div class="tb-fields-and-text" data-toolset-blocks-fields-and-text="1"><p>[types field='gun-type-number' format='FIELD_VALUE'][/types] [types field='gun-type'][/types] [wpv-conditional if="('weight-of-shot' ne '')"]([types field='weight-of-shot' format='FIELD_VALUE'][/types] [types field='weight-of-shot-unit'][/types] shot)[/wpv-conditional]</p></div>
<!-- /wp:toolset-blocks/fields-and-text -->

I can send you the credentials if you still need them, but in the meantime, this is the code I tried after disabling the block editor. It doesn't work. Thoughts?

#1660171

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

In my opinion, it should work. I'll suggest minimizing the code a bit and trying to display only one field without any conditional shortcodes if it still does not work, that needs to be debugged, I'll need to access your website and maybe also a copy of the site for more debugging in my computer.

If it works, then add another field, then the conditional.

Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Let me know if it would be ok to take a copy of your website?

#1664549

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

My apologies for the late reply, I do not work on Sundays and Mondays.
I just tried to login with the provided username/password to no avail. I tried the FTP access too and they are also not working for me. Please double-check them and update the credentials in your last or next messages.

#1665327

My apologies. I have updated the login information in the previous message. It should work now.

#1665489

I also need to know how to set it to display only the values from the current post. I'm assuming this is done with the Query Filter section but I don't know what to put in which box to make that happen.

#1666377

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for the new credentials.

I am not aware of all the custom fields that are involved in the field groups and posts, but from what I understand, you want to display the information from the same post(Gun Type) as a list, right?

Currently, you are displaying the list of posts(Gyn Type) queried by the view in a list. Then each post does not have a list at all, check this screenshot hidden link

Please check our glossary to see the difference between views and content templates. Views are meant to display lists of posts that match a query filter. A content template is meant to display only one post, that's why we use a content template inside the loop. The loop will go through each post from the view and will apply the content template to it.
- https://toolset.com/glossary/

I updated the content template used inside the view to build a list from the information of the post(Gun Type), hopefully it will give you an idea and you will put each field on the list as you want/intend. Check this screenshot hidden link
Check this HTML article about lists: hidden link

Regarding your last question, the query filter is tied to a view, it generates the underlying database query and returns a list of posts and their custom fields. A query filter have nothing to do with a content template or how a post is displayed. It is used to query the database. For example, a query filter can be "The list of posts, created by the logged-in user, with a certain status(draft, publish), assigned to a certain taxonomy term".

Hope this makes sense, let me know if you have any questions.

#1667081

I think I understand what you are saying.

Here's how it needs to be set up: I am building a content template to display information about shipwrecks (I am using Elementor's content template rather than the default Blocks templates, because I find the formatting easier to work with). Shipwreck is a custom post Type. I have a form (actually, series of forms) that allows people to enter information about the shipwreck. One of the types of information that needs to be entered is a list of the types of guns present on the ship. Because ships sometimes have all the same type of gun, and sometimes had 3 or more different types of guns, I would like to use a repeatable group, called gun type, to display a list of all gun types associated with that particular shipwreck.

I understand that Toolset treats repeatable groups like child posts, and I believe what you are saying is that each gun type is a 'post' of its own, and the View will display a list of different 'posts' (the different gun types), each with the fields assigned to it (number, type, weight of shot, weight unit).

My problem, however, is that I am getting a list of all gun types for all shipwrecks. That is, after creating a second shipwreck entry, that post - Shipwreck 2 - displayed the list of gun types entered for the first shipwreck, Test Shipwreck, even though no gun types had been associated with Shipwreck 2.

1. I need the View to display only the gun types associated with (children of) the current shipwreck post where the View is shown - that is, on the Test Shipwreck post, the View should display a list of the gun types associated with it, while on the Shipwreck 2 post, it should not display any gun types, because no information is known about Shipwreck 2's guns.

2. I also need to know how to reference the gun types so that I can tell the Content Template not to display the View if there is no data in it. I am working with a plugin (Dynamic Content for Elementor) that allows me to enter custom php code for conditional display. How do I call the four fields - number, type, weight of shot, weight unit - of the gun type 'post' to check if they contain data? If there are multiple repeatable groups (I plan on having a second one, for Excavation Projects), how do I call the fields for other groups?

#1668147

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for your feedback. Please check below my comments on your last questions:

1. I need the View to display only the gun types associated with (children of) the current shipwreck post where the View is shown - that is, on the Test Shipwreck post, the View should display a list of the gun types associated with it, while on the Shipwreck 2 post, it should not display any gun types, because no information is known about Shipwreck 2's guns.

The view "Gun Type List" needs to have a query filter that will make sure to return only the child posts of the current post. As this is used inside an Elementor template, we need to choose the current post in the loop with the correct relationship(parent-child). Check this screenshot that shows the correct relationship, and the option to choose(it is hidden by the dropdown) hidden link

2. I also need to know how to reference the gun types so that I can tell the Content Template not to display the View if there is no data in it. I am working with a plugin (Dynamic Content for Elementor) that allows me to enter custom php code for conditional display. How do I call the four fields - number, type, weight of shot, weight unit - of the gun type 'post' to check if they contain data? If there are multiple repeatable groups (I plan on having a second one, for Excavation Projects), how do I call the fields for other groups?

I am not aware of how is the "Dynamic Content for Elementor" works and what you would like to do. From what I understand so far, you want to display the children's posts only if there are any, if no child is present, you want to hide the view, right?
If that's the case, it can be done by different ways:
- If you want to implement it inside the view, you can use a condition on the number of results. Check the wpv-items-found shortcode and how we can implement conditions in Toolset view on the following articles:
The "wpv-items-found" shortcode: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-153272
The conditional output: https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/
- If you need to pass this information to the "Dynamic Content for Elementor" plugin or other parts of your website, you will need to create a custom function that returns the number of child posts. This function can use our relationship API, especially the toolset_get_related_posts function
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Check the following snippets, they may give you some examples on how to count the number of children posts(Gun Type)
- hidden link
- hidden link

I hope this answers your questions. Let me know if you still need any information.

#1668471
Annotation 2020-06-18 105205.jpg

1. Thank you, that answered by question about the query filter. It is working correctly now.

2. In my view, I need the parentheses and the word "shot" to not appear if there is no data specified in the weight of shot and weight unit fields (circled)

3. I need to fix the spacing so there is no gap between the posts listed in the View (brackets)

4. From what I understand so far, you want to display the children's posts only if there are any, if no child is present, you want to hide the view, right?
Yes, this is what I am trying to do. I need to write a snippet of php code that returns 'true' if there is data in a specific repeatable group (if there are children posts of a specific type such as Gun Type). But if there are multiple repeatable groups (Gun Type, Excavation Project) assigned to one Custom Post Type (Shipwreck), how do I specify this with php? I don't just want it to return true if there are *any* child posts, I want it to return true if there are child posts created using a given repeatable group.

#1668579

2. I figured this one out. The code needed to be:

<!-- wp:toolset-blocks/fields-and-text -->
<div class="tb-fields-and-text" data-toolset-blocks-fields-and-text="1"><p>[types field='gun-type-number' format='FIELD_VALUE'][/types] [types field='gun-type'][/types] [wpv-conditional if=" '[types field="weight-of-shot"][/types]' ne '' "]([types field='weight-of-shot' format='FIELD_VALUE'][/types] [types field='weight-of-shot-unit'][/types] shot)[/wpv-conditional]</p></div>
<!-- /wp:toolset-blocks/fields-and-text -->

I still need assistance with the other questions

#1668723

3. I figured this one out also, so I just need a reply for number 4.

My code was:

<!-- wp:toolset-blocks/fields-and-text -->
<div class="tb-fields-and-text" data-toolset-blocks-fields-and-text="1">[types field='gun-type-number' format='FIELD_VALUE'][/types] [types field='gun-type'][/types] [wpv-conditional if="('[types field="weight-of-shot"][/types]' ne '')"]([types field='weight-of-shot' format='FIELD_VALUE'][/types] [types field='weight-of-shot-unit'][/types] shot)[/wpv-conditional]</div>
<!-- /wp:toolset-blocks/fields-and-text -->
#1669693

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Awesome 🙂

For the custom code, the function or shortcode you will create will use the toolset_get_related_posts function. The function needs to have the relationship slug. In the case of repeatable groups, you will need to pass the repeatable field slug for each of your repeatable group.
This means, that each repeatable field group will be counted alone, the function will not count all the repeatable groups at once.

I hope this makes sense. If it does not, please try to make it for the first repeatable group and let me take a look at it. If it does not work we'll find out why.

#1672525

It makes sense, but I'm fairly new to php so bear with me. This is what I have so far:

$guntypeids = toolset_get_related_posts( 
      get_the_id(),
      array('shipwreck','gun-type'),
      [
        'query_by_role' => 'parent',
        'role_to_return' => 'child',
        'need_found_rows' => true,
      ]
    );
$guntypecount = $guntypeids ['found_rows'];
if ($guntypecount > 0) {
return true;
}

Here's the sample code the Dynamic.ooo plugin provides: hidden link

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