Last active
October 11, 2018 12:48
-
-
Save mgibbs189/d92ebcaeb0aade38859a to your computer and use it in GitHub Desktop.
Filter results to children post items (Types plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment