I have a view that loads some "Project" custom post types.
When used on a certain page, I want it to query filter based on a relationship query filter.
When used on another page, I want to have no query filter and just load the most recent few projects.
Is that possible without making a duplicate view? It seems like the query filter is attached to the root view and not configurable based on where it's being used but maybe there's a work around?
-ALSO-
Is there a way to have a compound OR filter? Eg "Give me results that match this, but if there are none, then just give me any 3"
Hi Will,
Thank you for contacting us and I'll be happy to assist.
To remove the effect of a relationship query filter from a view, when it is used on a specific page, you can add the following code, into your active theme's "functions.php" file:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'filter_relationship_custom_fn', 1000 , 3 );
function filter_relationship_custom_fn( $query_args, $view_settings ) {
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 123) ) {
if( (is_page(456)) && (array_key_exists('post__in', $query_args)) ) {
unset($query_args['post__in']);
}
}
return $query_args;
}
Note: Please replace 123 with the actual view's ID that you'd like to target through the code and also replace 456, with the actual page's ID where you'd like to ignore the post-relationship filter.
There is no built-in feature to pre-filter the view's query based on the number of results returned. But a neater workaround is to create a separate view for the case of "any 3 items" and include its shortcode within the "[wpv-no-items-found]....[/wpv-no-items-found]" shortcode, inside the main view's "Loop Editor" section.
( screenshot: hidden link )
As a result, if the main view's query won't have any items to show based on the set criteria, the nested view's output will show.
I hope this helps and please let me know if you need any further assistance around these points.
For a new question/concern, please open a new ticket.
regards,
Waqar