Views plugin provides an API, making it easy to display Views output using PHP.
When you ask for help or report issues, make sure to tell us all related information about your View and the data that you want to display using the Views API.
Problem: I have a View of custom posts that displays a map with markers, using a distance filter and sorting results by distance. In some cases the distance center is set by a URL parameter, and in others, it is set by a shortcode attribute. I have implemented some custom code in a wpv_view_settings filter to toggle between the shortcode attribute and the URL parameter. In the cases where distance center is set by a shortcode attribute, I am using another custom shortcode to get the User's predicted zip code. That shortcodes an external service to get a predicted zip code based on the User's IP address. The custom zip code service seems to be working correctly, and the results of the View seem to be accurate.
However, in the cases where this zip code shortcode is used to set the View's distance Query Filter by shortcode attribute, the View consistently causes the page generation time to jump by 3 or more seconds. I would like to try to optimize the page load time as much as possible.
Solution: In this case, testing revealed that using the custom zip shortcode inside a wpv_view_settings filter callback was the source of the delay in page generation, for some reason not fully understood. Replacing that shortcode with a hard-coded zip code resulted in a page generation time just a fraction of a second higher than the page without the View present at all.
Optimization of the zip code shortcode is necessary to prevent the latency seen in page generation time.
Problem: If no items are found in a View of WooCommerce Orders, I would like to automatically refresh the View results after a set amount of time.
Solution: There is no automatic feature for refreshing View results, nor is there a true JavaScript API for Views, but you could achieve something similar by reloading the current page in a setTimeout function added to a script tag in the wpv-no-items-found block of the View, for example:
This will reload the current page after 5 seconds if no results are found in the View. That loop of refreshing every 5 seconds would continue until results exist.
Problem: I have a View that I would like to filter by post author. That View may be displayed in a BuddyPress profile. I have a PHP function provided by BB that will help determine the User ID based on the profile being displayed.
Solution: Use the Views Filter API wpv_filter_query to programmatically set a post author filter in this View using the function provided by BuddyPress / BuddyBoss.
// Override the post author filter of a View shown in the BP User page to display only that User's posts.
// https://toolset.com/forums/topic/filtering-views-query-by-author-for-a-profile-page/
add_filter( 'wpv_filter_query', 'tssupp_bp_force_author_filter',99,3 );
function tssupp_bp_force_author_filter( $query_args,$views_settings, $view_id) {
$views = array( 123, 456 );
if ( in_array( $view_id, $views ) ){
// post author equals bp_displayed_user_id() if function exists
$query_args['author'] = function_exists('bp_displayed_user_id') ? bp_displayed_user_id() : 0;
}
return $query_args;
}
You would replace 123, 456 with a comma-separated list of View IDs which you would like to filter by post author. This post author filter will only work if the function provided by BP/BB returns the correct User ID. If you try to place this View on a page where the BP/BB function does not return the correct value, the View will not be filtered appropriately.
Problem: I have created an archive of WooCommerce Products that includes a wpv-woo-product-images shortcode to display the Product Image Gallery in each listing. The page itself loads quickly, but it seems that the results of the archive are hidden for a few seconds before they appear. I would like to display the results quickly.
Solution: To prevent the hidden listings upon page load, turn off the "Preload images before transition" option in the Archive's Pagination and Sliders panel > Advanced Settings area.
Problem: I created two CPTs: Events and Profiles. I have a View of Events displayed in the template for Profile posts, and I would like to only display Events where the title of the current Post can be found in the "notes" WYSIWYG field on the Event post.
Solution: Configure the notes custom field Query Filter to use a shortcode attribute and pass the current Profile post title into that shortcode attribute like so:
Problem: I would like to prevent search results from appearing until a custom search filter is added.
Solution: Use our PHP Views Filter API wpv_filter_query_post_process to drop a View's results until some meta_query is added. The following custom code can be used as a template:
/**
* No initial results until a filter has been applied
* Tests for one specific custom field search filter
* Reference: https://toolset.com/forums/topic/how-to-disable-the-search-in-view-using-blank/
*/
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
$target_views = array( 123, 456 );
$field_slug = 'field-slug';
// you should not edit below this line
if ( in_array( $view_id, $target_views ) ) {
// blank search indicates just blank space in the filter
$blank_search = isset($query_results->query['meta_query']) && sizeof($query_results->query['meta_query']) == 2 && $query_results->query['meta_query'][0]['key'] == 'wpcf-'.$field_slug && trim($query_results->query['meta_query'][0]['value']) == '';
// if there is no search term set, drop all results
if ( !isset( $query_results->query['meta_query'] ) || $blank_search ) {
$query_results->posts = array();
$query_results->post_count = 0;
$query_results->found_posts = 0;
}
}
return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
Problem: I would like to display a View of related posts in an M2M relationship. There is checkbox in the intermediary post type (in the relationship) and I would like to display only those related posts where the checkbox is unchecked.
Solution: This requires some custom code, and it also requires that the View is configured to display the intermediary post type. Add the following code snippet to filter by an unchecked checkbox:
add_filter( 'wpv_filter_query', 'unchecked_ints',99,3 );
function unchecked_ints( $query_args,$views_settings, $view_id) {
global $post;
$field_slug = 'release-artist-checked';
$views = array( 48324 ); /* "Collaborations" */
$ints_args = array(
'query_by_role' => 'child',
'role_to_return' => 'intermediary',
'limit' => 1000,
'offset' => 0,
'args' => [
'meta_key' => 'wpcf-' . $field_slug,
'meta_value' => 1,
'meta_compare' => '='
]
);
$ints = toolset_get_related_posts( $post->ID, 'supporting-artists', $ints_args);
// you should not need to edit below this line
if ( in_array( $view_id, $views ) ){
$query_args['post__not_in'] = $ints;
}
return $query_args;
}