Skip Navigation

[Resolved] Showing recent posts through 3 different views on the page

This support ticket is created 4 years, 3 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: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by RobertB7373 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1754521

Hi Waqar,

I have 2 questions regarding "Views":

1.) Basically on each page I have 3 individual and independent “views”. On the first “view” I would like to display the latest post from the the other 2 “views” but of course they should have to know which one appears on the first ‘“view” and they should skip that one.
Every post have a checkbox for “Recommended” article, if it’s selected, the first “view” should skip the latest articles from the other 2 “views” and show just the recommended article. In that scenario the other 2 “views” should show the latest article as well without skipping them.

2.) It’s unclear for me if the “Cache View” should be toggled “on” or “off” where I’m showing just 4 items, so I don’t have pagination.

Website: hidden link

Thank you again,
Robert

#1756287

Hi,

Thank you for waiting.

During testing on my website, I was able to exclude the most recent post that is shown through the first view using the "wpv_filter_query" filter:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )


add_filter( 'wpv_filter_query', 'filter_to_exclude_home_posts_fn', 1000 , 3 );
function filter_to_exclude_home_posts_fn( $query_args, $view_settings ) {

	$view_ids = array(1234, 5678);
	if ( (!is_admin() && isset($view_settings['view_id']) ) && ( in_array($view_settings['view_id'], $view_ids) ) )
	{
		$args = array(
				'post_type'        => array('post-type-1-slug', 'post-type-2-slug'),
				'numberposts'      => 1,
				'post_status'      => 'publish',
				'orderby'          => 'date',
				'order'            => 'DESC',
				);

		$posts_array = get_posts( $args );

		if(!empty($posts_array)) {
			$post_to_exclude = $posts_array[0]->ID;
			$query_args['post__not_in'][] = $post_to_exclude;
		}
	}

	return $query_args;
}

Please replace "1234" & "5678", with the IDs of your second and the third views, respectively. You'll also need to replace "post-type-1-slug" & "post-type-2-slug" with the actual slugs of your two post types.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

This custom function will get the ID of the latest post (ordered by post date) from the two post types combined (which will be shown by the first view) and then set it to be excluded from the results of the second and the third views.

The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1760481
screenshot.png

Hello Waqar,

The code works very well on one page.. lets say "Carte" hidden link it works as expected, thank you!... but I can't apply to the other 2 main pages "Oameni" and "Arte". I've tried to extend the view id's and post type slugs but it doesn't seems to work.

Could you help me a bit more? just to apply to the other 2 pages.

Thank you,
Robert

#1762073

Hi Robert,

Thanks for the update and glad that the code works.

Instead of handling multiple views for multiple pages through a single code snippet (like in your screenshot), you should add a separate snippet for each page.

For example, suppose on your "carte" page the IDs of two views are 123 and 456 and the post types to get the latest post to exclude from are "post-type-1-slug" and "post-type-2-slug", then the snippet for this page would like this:


add_filter( 'wpv_filter_query', 'filter_to_exclude_carte_posts_fn', 1000 , 3 );
function filter_to_exclude_carte_posts_fn( $query_args, $view_settings ) {
 
    $view_ids = array(123, 456);
    if ( (!is_admin() && isset($view_settings['view_id']) ) && ( in_array($view_settings['view_id'], $view_ids) ) )
    {
        $args = array(
                'post_type'        => array('post-type-1-slug', 'post-type-2-slug'),
                'numberposts'      => 1,
                'post_status'      => 'publish',
                'orderby'          => 'date',
                'order'            => 'DESC',
                );
 
        $posts_array = get_posts( $args );
 
        if(!empty($posts_array)) {
            $post_to_exclude = $posts_array[0]->ID;
            $query_args['post__not_in'][] = $post_to_exclude;
        }
    }
 
    return $query_args;
}

Similarly, suppose on your "Oameni" page the IDs of two views are 789 and 101112 and the post types to get the latest post to exclude from are "post-type-3-slug" and "post-type-4-slug", then the snippet for this page would like this:


add_filter( 'wpv_filter_query', 'filter_to_exclude_oameni_posts_fn', 1000 , 3 );
function filter_to_exclude_oameni_posts_fn( $query_args, $view_settings ) {
 
    $view_ids = array(789, 101112);
    if ( (!is_admin() && isset($view_settings['view_id']) ) && ( in_array($view_settings['view_id'], $view_ids) ) )
    {
        $args = array(
                'post_type'        => array('post-type-3-slug', 'post-type-4-slug'),
                'numberposts'      => 1,
                'post_status'      => 'publish',
                'orderby'          => 'date',
                'order'            => 'DESC',
                );
 
        $posts_array = get_posts( $args );
 
        if(!empty($posts_array)) {
            $post_to_exclude = $posts_array[0]->ID;
            $query_args['post__not_in'][] = $post_to_exclude;
        }
    }
 
    return $query_args;
}

Similarly, you can keep adding separate filter/function snippets with respective view IDs and post type slugs, for more pages.

Note: If you're finding it difficult to work with these PHP code snippets on your own, you can hire a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1763099

My issue is resolved now. Thank you!