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.
Viewing 15 topics - 271 through 285 (of 422 total)
Problem: I have a View that filters based on shortcode attributes. I would like use a JavaScript templating library like Handlebars to set the View's shortcode attribute with data from a JSON object that exists on the page when the page loads.
Solution: Unfortunately there is no JavaScript API for forms, so there is not an easy way to set a shortcode attribute value with JavaScript after the View has loaded. If the JSON data is available in PHP before the page loads, then you may be able to use the PHP API render_view to set the desired attribute value and render the View programmatically instead of using the wpv-view shortcode:
Problem: I would like to show no initial results in my View, but the code snippet doesn't seem to be working correctly. I have one Query Filter predefined.
Solution: You would have to modify the custom code to account for that one Query Filter. The following modification tests the number of Query Filters, and only shows results if the User has selected another filter on the front-end:
add_filter( 'wpv_filter_query_post_process', 'tssnippet_no_initial_results_one_pre', 10, 3 );
function tssnippet_no_initial_results_one_pre( $query_results, $view_settings, $view_id ) {
$target_views = array( 4407 );
if ( ! in_array( $view_id, $target_views ) ) {
return $query_results;
}
// if there is only the one predefined custom field filter plus the relation, drop all results
if (
sizeof($query_results->query['meta_query']) == 2
&& ! isset( $query_results->query['tax_query'] )
&& ! isset( $query_results->query['s'] )
) {
$query_results->posts = array();
$query_results->post_count = 0;
$query_results->found_posts = 0;
}
return $query_results;
}
Problem: I would like to create a front-end filter where the User can enter a number. Then I would like to filter the results such that one custom field is the minimum value and another custom field is the maximum value, and the User-entered number exists between that range.
Solution: There's nothing exactly like this in Views, so it will require a custom numeric input field and custom code using the Views API wpv_filter_query. An example is shown below:
add_filter('wpv_filter_query', 'between_custom_field_values', 99, 3);
function between_custom_field_values($query_args, $view_settings, $view_id ) {
$view_ids = array( 123, 456 ); // change this to a comma-separated list of the View ids you want to filter
if( in_array( $view_id, $view_ids)) {
$range_number = isset($_GET['range_number']) ? $_GET['range_number'] : null;
if ($range_number !== null) {
$query_args['meta_query'][] = array(
'relation' => 'AND',
array(
'key' => 'wpcf-maxslug',
'value' => $range_number,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'wpcf-minslug',
'value' => $range_number,
'compare' => '<=',
'type' => 'NUMERIC'
)
);
}
}
return $query_args;
}
Problem: I would like to display the same single post using different Content Templates based on the referring link in a View. I have two Views that will be displayed in different locations, and depending on which View is the referrer I'd like to display a different Content Template.
Solution: Use a URL parameter to set a specific template ID: