Views API Functions
Sometimes, you might need to display Views output using PHP. For example, if you are building a membership site and need user account pages to display content that belongs to a user, you might use a View for that.
These functions let you display and use Views output in your code.
Returns a result of a query filtered by a View as an array of post objects.
$view_id
- Integer value for the ID of the View that filters the query results.
$post_in
- WP_Post object that sets the global post.
$current_user
- WP_User object that sets the global user.
$args
- Optional array of attributes to pass to the View, like shortcode attributes when using the wpv-view shortcode.
The result of the query.
More Usage examples
// Get the results of the View with ID equal to 40 and output the post titles $filtered_posts = get_view_query_results( 40 ); foreach ( $filtered_posts as $filtered_post ) { echo $filtered_post->post_title; }
Returns the ID of the content template assigned to a given post, or zero if there is no template assigned.
$post_in
- The ID of the post to get the information from.
More Usage examples
The ID of the assigned Content Template or 0 if there is no assigned template.
// Returns the ID of the assigned Content Template or 0 if there is no one $has_ct_assigned = has_wpv_content_template( 45 ); if ( $has_ct_assigned > 0 ) { // Has a Content Template assigned. } else { // No Content Template is assigned. }
Renders a View and returns the HTML output.
$args
- An array of parameters, where following keys can be passed:
name
- The View post_nametitle
- The View post_titleid
- The View post IDtarget_id
- Only the View Search is shown. The results will be shown on the page with ID passed to the attribute.The HTML output of the view.
More Usage examples
//Example 1 $args = array( 'title' => 'My View name', 'myattribute' => 'myvalue' ); echo render_view( $args ); //Example 2 - Display the results on a page with specified ID $args = array( 'title' => 'Your View Title', 'target_id' => '19', //The ID of the page where to display results ); echo render_view( $args );
Returns the content of a Content Template applied to a post.
$view_id
- Integer value of the the ID of a relevant Content Template.
$post_in
- WP_Post object that sets the global post.
$current_user
- WP_User object that sets the global user.
The content of the Content Template.
More Usage examples
//Renders the post $mypost using the Content Template with ID equal to 80 echo render_view_template( 80, $mypost );