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