I have a view that displays a repeatable field group. It select posts in Any relationship that are related to the Post with ID set by a shortcode attribute.
If that Post ID doesn't exist, the view should display a "No results" message. Instead, it's returning all the posts that are on the wp_toolset_connected_elements table (even if they are not related at all).
Hi,
Thank you for contacting us and I'd be happy to assist.
Your observation is correct and this is the default behaviour. But, the 'wpv_filter_query' can be used to change that:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
add_filter( 'wpv_filter_query', 'filter_posts_specificmix_check', 1000 , 3 );
function filter_posts_specificmix_check( $query_args, $view_settings ) {
// check if specific view
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
global $WP_Views;
$attributes = $WP_Views->view_shortcode_attributes;
// if 'specificmix' attribute value
if ($attributes[0]['specificmix'] <= 0 ) {
$query_args['post_type'] = 'xyz';
}
}
return $query_args;
}
Note: You'll replace '12345' with the actual ID of your target view.
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 the active theme's "functions.php" file.
This function will check if a specific view is being viewed and if 'specificmix' attribute value is less than or equal to '0', will change the post type to a fake post type 'xyz', which will show no results.
regards,
Waqar