Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active October 11, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/d92ebcaeb0aade38859a to your computer and use it in GitHub Desktop.
Save mgibbs189/d92ebcaeb0aade38859a to your computer and use it in GitHub Desktop.
Filter results to children post items (Types plugin)
<?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