Skip Navigation

[Resolved] Limit number of posts of each type in a mixed View

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

Problem:
A View queries two different posts types. How is it possible to set a limit to show 3 of one kind and 3 of the other?

Solution:
If it not possible in Views (nor would it be possible creating the query directly with the built-in WP_Query).

It is necessary to create two Views, one for each post type, each with its own limit.

This support ticket is created 6 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.

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

Last updated by jozsefG 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#638567

Hello

I try to create a view from two CPT's posts. My problem is that I want to show only 3 posts for each CPT in the same grid.
One is called destinations and should show the featured post that are set in a post field.
The other is called activities, and this should be show 3 random posts.

Can I achieve this inside the same view, and how?

#638710

Nigel
Supporter

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

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

Hi there

This wouldn't be possible if you were writing the queries in PHP with the built-in WP_Query class, and so is not possible in Views.

In either case you would need to create two separate queries.

So create one View which shows three featured destinations, and a second View which shows 3 random activities, which you can insert one after the other.

If that is problematic for some reason, let me know.

#642378

I have the two separate view, and I thought maybe I can put them one after the other because I need them in the same grid and I could style it with flex easily. But I have to get rid of this wrapper first:

<div id="wpv-view-layout-1653-TCPID1655"...

I will create this manually before and after the code block I put the two views shortcode in. So is there a way to have a view that just outputs my divs one after the other without the wrapper?

#642499

Nigel
Supporter

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

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

That's the issue I thought you might run up against, depending on your implementation of a grid.

You can produce a "naked" View output without the wrapper DIV using the following code, which you should add to your theme's functions.php file or using a plugin such as Code Snippets:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'tssupp_clean_view_output', 5, 2 );
function tssupp_clean_view_output( $out, $id ) {

	$views_to_clean = array( '123', '124' ); // EDIT

	if ( in_array( $id, $views_to_clean ) ) {
		$start = strpos( $out, '<!-- wpv-loop-start -->' );
		if ( $start !== false && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false ) {
			$start = $start + strlen( '<!-- wpv-loop-start -->' );
			$out = substr( $out , $start );
			$end = strrpos( $out, '<!-- wpv-loop-end -->' );
			$out = substr( $out, 0, $end );
		}
	}
	return $out;
}

Edit the IDs of the Views you want to clean.

#642707

Wow, this is great 🙂 It works perfectly 😀 😀 Now I have two views in the same grid: hidden link

Thank you very much, your help was great!!