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 - 256 through 270 (of 418 total)
1) Setup a post view, query posts, without any filter.
2) use "wpv_filter_query" to trigger a custom PHP function, in this PHP function, use your custom SQL query to get those specific post IDs, then pass the value as parameter "post__in" of query
Problem: I would like to create a Query Filter to show the most recent posts beginning 8 weeks ago and continuing to the present. I have used the post date filter feature to specify Weeks: PAST_ONE (8). That seemed to work well until the beginning of the new year, when the filter stopped showing posts from the previous year. So the filter only seems to work correctly when the date 8 weeks ago was in the same calendar year as today.
Solution: Remove the post date Query Filter in the view editor. Add a custom code snippet to filter by post_date using any PHPstrtotime-compatible date string:
Change 123, 456 to be a comma-separated list of View IDs where you want to apply this date filter. If you want to filter by a different start date, change 8 weeks ago to any strotime-compatible date string representing the start date.
Problem: I have custom fields in two different post types that contain similar values. I would like to filter post type A by the custom field value in post type B.
Solution: Use a Query Filter that responds to a shortcode attribute, and set the attribute value using the raw types field data from the current post type.
Problem: I have a View and I want to ensure that 4 results are always displayed. I can set a limit of 4, but I would also like to know how to append random results as needed if the number of results is less than 4.
Solution: Use the Views Filter wpv_filter_query_post_process to manipulate the result set programmatically. Use WP_Query to fetch random results that aren't included in the results already, and then append those to the current results. Example:
add_filter( 'wpv_filter_query_post_process', 'nh_modify_related_gt_query', 10, 3 );
function nh_modify_related_gt_query( $query, $view_settings, $view_id ) {
if( $view_id = '22313' ) { // Applies to "Related Glossary Terms (4)" view
if ( sizeof( $query->posts ) < 4 ) { // if the query has less than 4 related terms
$fillsize = 4 - sizeof( $query->posts );
$post_ids = wp_list_pluck( $query->posts, 'ID' );
$args = array(
'post_type' => 'glossary',
'posts_per_page' => $fillsize,
'post__not_in' => $post_ids,
'orderby' => 'rand',
'no_found_rows' => true
);
$fillquery = new WP_Query( $args );
$initposts = $query->posts;
$randposts = $fillquery->posts;
$filled_terms = array_merge($initposts, $randposts);
$query->posts = $filled_terms; // add the random posts at the end of the posts result array
$query->found_posts = 4; // modify the count of found posts
$query->post_count = 4; // modify the count of displayed posts
}
}
return $query;
}