I'm working on a study site in WordPress and I'm using custom post types.

The Custom Post Type Course is responsible for creating the courses, displayed in single-courses.php, where the basic information of the cuso is displayed, this Custom Post has two taxonomies: categories and tags.

And the Custom Post Type Lesson creates the course lessons and is displayed in single-lesson.php.

I need to display the contents of the Custom Post Type Lesson in the Custom Post Type Course and I am not able to do that.

Remembering that the slug of the Custom Post Type Course is my-site.com/course, where all the courses are displayed and my-site.com/course/course-name where the course information is presented.

Course name and the lesson should follow the same structure my-site.com/course/course-name/lesson-1

  • How are you recording the relationship between a lesson and a course? (how do you know that this lesson is related to that course?) Post meta?
    – Jen
    Nov 22, 2016 at 20:27
  • @guiniveretoo Yes, I am using a post meta ("course_id") in which the lesson will be linked to the course. I had thought about the possibility of developing a course selector on the page lesson editor, but I do not know if I will have time for it. Nov 22, 2016 at 20:35
  • What have you tried so far? "> I need to display the contents of the ... Lesson in the ... Course and I am not able to do that."
    – Jen
    Nov 22, 2016 at 20:58
  • Have you already tried Pods? This is a fantastic framework to create and manage custom post types. With Pods, it's easy to create relations between post types, too. Which is, what you'll most likely need.
    – Bunjip
    Nov 22, 2016 at 21:39

1 Answer 1


Base on comment, you need to create a little function to display these related posts. You can do it in different ways, here is an simple example by adding your own action to the template.

in your template file place where you want to show related lessons


In functions.php

function get_related_lesson(){
    global $post;

    $args = array(
              'posts_per_page' => 6,
              'post_status'=> 'publish',
              'meta_query' => array(
                    'key'     => 'course_id',
                    'value'   => $post->ID,
                    'compare' => 'LIKE',
    $related_posts = new WP_Query($args);

    while ( $related_posts->have_posts() ) {
       echo '<li><a href="' .get_permalink($related_posts->post->ID). '">' . get_the_title( $related_posts->post->ID ) . '</a></li>';

    // Restore original Post Data
add_action('related_lesson', 'get_related_lesson');

You can also use the_content filter.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.