Skip Navigation

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.

get_view_query_results

Description

Returns a result of a query filtered by a View as an array of post objects.

Arguments
  • $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.

Output

The result of the query.

More Usage examples

Example

// 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;
}

has_wpv_content_template

Description

Returns the ID of the content template assigned to a given post, or zero if there is no template assigned.

Arguments
  • $post_in - The ID of the post to get the information from.

Output

More Usage examples

Example

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.
}

render_view

Description

Renders a View and returns the HTML output.

Arguments
  • $args - An array of parameters, where following keys can be passed:

    • name - The View post_name
    • title - The View post_title
    • id - The View post ID
    • target_id - Only the View Search is shown. The results will be shown on the page with ID passed to the attribute.
Output

The HTML output of the view.

More Usage examples

Example

//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 );

render_view_template

Description

Returns the content of a Content Template applied to a post.

Arguments
  • $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.

Output

The content of the Content Template.

More Usage examples

Example

//Renders the post $mypost using the Content Template with ID equal to 80
echo render_view_template( 80, $mypost );