Skip Navigation

[Resolved] Using views within PHP

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a View using PHP. The View displays sibling posts of the current post using a shortcode attribute in the post relationship filter.

Solution: Use the post relationships API to get the parent post ID, then pass that value into the render_view API using the shortcode attribute name as the key.

global $post; // the current episodic review post object. Depending on how you implement this code you may need to change this
$parent_id = toolset_get_related_post( $post->ID, 'game-with-episodic-content', 'parent', array(), null );
$args = array(
    'name' => 'related-episodic-reviews-sidebar',
    'wpvrelatedto' => $parent_id
);
echo render_view( $args );

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/views-api/#render_view

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by michaelS-53 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1231107

Tell us what you are trying to do?
I'm trying to use the shortcode that Christian helped create and implement it in PHP using render_view.

[wpv-view name='related-episodic-reviews-sidebar' wpvrelatedto='[wpv-post-id item="@game-with-episodic-content.parent"]

Is there any documentation that you are following?
Using this as reference: https://toolset.com/documentation/programmer-reference/views-api/#render_view

I'm not where to pass the @game-with-episodic-content.parent part in the render_views.

#1231120

You can use the post relationships API to get the parent post ID, then pass that value into the render_view function using the shortcode attribute name "wpvrelatedto" as the key. Here's an example for rendering this View in PHP:

global $post; // the current episodic review post object. Depending on how you implement this code you may need to change this
$parent_id = toolset_get_related_post( $post->ID, 'game-with-episodic-content', 'parent', array(), null );
$args = array(
    'name' => 'related-episodic-reviews-sidebar',
    'wpvrelatedto' => $parent_id
);
echo render_view( $args );
#1231121

My issue is resolved now. Thank you!