0

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

| improve this question | | | | |
  • 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 '16 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. – Jamil Filho Nov 22 '16 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 '16 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 '16 at 21:39
0

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

do_action('related_lesson');

In functions.php

function get_related_lesson(){
    global $post;

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

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

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

You can also use the_content filter.

Hope it helps.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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