Skip Navigation

[Resolved] Single view for multiple post types

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

Problem:
The customer wanted one View to show the latest post for each of three taxonomy terms, plus the latest "Featured" post from another post type, in a 4-item grid. They also asked how to add a class to the wpv-view shortcode.

Solution:
One View cannot return the latest post from each group, because its limit applies to all results combined. Create four small Views, each with a limit of 1, ordered by post date descending, with a taxonomy or custom field filter. Place the four shortcodes inside a container div in a single Custom HTML block, which WordPress does not wrap, and style it as a grid. The wpv-view shortcode has no class attribute, so use a wrapper div.

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.

This topic contains 4 replies, has 1 voice.

Last updated by - 23 hours, 52 minutes ago.

Assisted by: Christopher Amirian.

Author
Posts
#2919709

Is it possible to have a view for multiple post types? For example one view that pulls the latest post from two different post types.

Post type A. Pull the the latest of the following:

- Latest post with taxonomy "Fruit"
- Latest post with taxonomy "Car"
- Latest post with taxonomy "Building"

Post type B will pull:
- Latest post with checkbox "Featured" checked

The idea is to have a 4 grid layout that will display the most recent content from the above content in one view.

Also, how can I add a class to the wpv-view shortcode?

#2919726

Christopher Amirian
Supporter

Languages: English (English )

Hello,

A View can query more than one post type. But its limit applies to all its results together, so one View can't return "the latest post" from each of four different groups.

The reliable way is to create four small Views, each showing only one post, and place them side by side in your grid.

Please follow the steps below:

1. Go to Toolset > Views and add a new View.

2. In Content Selection, select post type A.

3. In Query Filter, add a taxonomy filter for the term "Fruit".

4. In Ordering, set Order by to Post date and choose Descending.

5. In Limit, set it to show 1 item.

6. Save the View.

7. Repeat these steps for "Car" and "Building".

8. For the fourth View, select post type B and add a custom field filter for the "Featured" checkbox, with the same ordering and limit.

9. Place the four View shortcodes in a four-column grid on your page.

There is no class attribute for the wpv-view shortcode. To add a class, wrap the shortcode in a div, for example:

<div class="my-grid-item">[wpv-view name="latest-fruit"]</div>

Then you can style .my-grid-item with your own CSS.

Thanks.

#2919731

Thank you Christopher. I was hoping to not need to wrap the wpv-view in another div since I am trying to apply a flex layout, but I guess I'll need to target the direct child of the wrapping div.

As for the multiple views, I'm using worpdress blocks and didn't to want to have wordpress block wrap each view with extra div wrappers. I needed each wpv-view div directly next to each other. My current workarround was to create an additional view that will have no results but would then hold the 4 small views within the [wpv-no-items-found] portion of the extra view. Is that ok or could it cause problems? Please let me know if there is any better solution.

#2919752

Hello,

Your current setup will work, but it runs an extra query, and it depends on the parent View always returning no results. If that View ever finds something, the four Views will disappear.

You can get the same result without a parent View by using a single Custom HTML block. WordPress adds no wrapper around it, so each View's output is a direct child of your flex container.

Please follow the steps below:

1. Edit the page and add a Custom HTML block.

2. Put your flex container and the four View shortcodes inside it, for example:

<div class="my-grid">[wpv-view name="latest-fruit"][wpv-view name="latest-car"][wpv-view name="latest-building"][wpv-view name="latest-featured"]</div>

3. Style .my-grid as your flex container, and target its direct children for each item.

4. Remove the parent View you used as a holder.

Please note that if a View includes a search form, the form is output too, as an extra item in the grid. Views without search filters output only their results.

Thanks.

#2919921

Good idea! thanks Christopher