Filter results to children post items (Types plugin)

mgibbs189

{{gist file=”gistfile1.php” lang=”php”}}
<?php
/**
* Context:
* Using the Types plugin, the “Resources” post type is a child of the “Courses” post type.
* On a Course page, show only Resources belonging to the current Course
*
* _wpcf_belongs_courses_id
* This custom field stores the ID of a Resource’s parent Course
*/

// Get the Course slug from the URL
$uri = explode(‘/’, $this->http_params[‘uri’] );
$slug = array_pop( $uri );

// Get the Course ID from the slug
$result = get_posts( array( ‘post_type’ => ‘courses’, ‘name’ => $slug, ‘posts_per_page’ => 1 ) );
$course_id = empty( $result ) ? array() : $result[0]->ID;

return array(
‘post_type’ => ‘resource’,
‘posts_per_page’ => 100,
‘meta_query’ => array(
array(
‘key’ => ‘_wpcf_belongs_courses_id’, // Show Resources belonging to the the current Course
‘value’ => $course_id,
)
)
);
{{/gist}}