Using meta data to retrieve related posts – a way to use nested relations?
I am modifying a site that currently has various custom post types. The structure of one to many relationships is basically
course
lesson
lesson
topic
lesson
quiz
topic
quiz
There are times when I want to get all quizzes associated with a lesson.
Currently the site uses individual queries to extract related data only between 2 post types.
For instance, to get quizzes for a lesson you have
$args = ['post_type' => 'sfwd-quiz',
'numberposts' => -1,
'orderby' => 'menu_order',
'meta_key' => 'lesson_id',
'meta_value' => get_the_id(),
'meta_compare' => '='];
$quizzes = get_posts($args);
Now, I would prefer at times to retrieve all quizzes for a lesson, which means quizzes for a topic in a lesson, since a topic is tied to a lesson as many to one.
So is there a way to chain relationships in get_posts() meta data queries?
Something that syntactically would be
$quizzes = get_posts(where post type is quiz and meta_lesson_id = $id, OR where post type is quiz and meta_topic_id is related to lesson with meta_lesson_id = $id)
Thanks,
Brian
Leave an answer