如何通过WP_Query获取LearnDash已发布课程及其子主题
Hey there! Let's tackle this LearnDash course and topic listing issue together. I know exactly how to pull in those sub-topics for each published course—here's what you need to know:
How LearnDash Links Courses to Sub-Topics
LearnDash stores the relationship between a course (sfwd-courses) and its sub-topics (sfwd-topic) using a post meta field on each topic. Specifically, every topic has a meta key course_id whose value is the post ID of its parent course. That's the key to connecting the two!
Updated Code to List Courses + Their Sub-Topics
I've expanded your existing code to include a nested query for each course's published topics. This will output each course along with its associated sub-topics in the order you set in the LearnDash Course Builder:
// Query for all published LearnDash courses $course_args = array( 'post_type' => 'sfwd-courses', 'post_status' => 'publish', 'order' => 'DESC', 'orderby' => 'ID', 'mycourses' => false, 'posts_per_page' => -1 // Show all courses; adjust this if you need pagination ); $course_loop = new WP_Query( $course_args ); // Check if we have courses to display if ( $course_loop->have_posts() ) : while ( $course_loop->have_posts() ) : $course_loop->the_post(); // Output course details (customize this to match your site's layout) echo '<div class="ld-course">'; echo '<h2>' . get_the_title() . '</h2>'; echo '<p>' . get_the_excerpt() . '</p>'; // Query for this course's published sub-topics $topic_args = array( 'post_type' => 'sfwd-topic', 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'menu_order', // Matches the order set in LearnDash Course Builder 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'course_id', // Meta key linking topic to its parent course 'value' => get_the_ID(), // Current course's post ID 'compare' => '=' ) ) ); $topic_loop = new WP_Query( $topic_args ); // Output the list of sub-topics if ( $topic_loop->have_posts() ) : echo '<h3>Course Topics</h3>'; echo '<ul class="ld-topics">'; while ( $topic_loop->have_posts() ) : $topic_loop->the_post(); echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; endwhile; echo '</ul>'; else : echo '<p>No topics have been added to this course yet.</p>'; endif; echo '</div>'; // Reset post data for the topic loop to avoid conflicts wp_reset_postdata(); endwhile; else : echo '<p>No published courses are available at this time.</p>'; endif; // Reset post data for the course loop wp_reset_postdata();
Key Notes About the Code
meta_query: This is the critical part that filters topics to only those associated with the current course. We're matching thecourse_idmeta value to the current course's ID.orderby=>menu_order: This ensures topics appear in the same order you arranged them in the LearnDash Course Builder. If you prefer ordering by ID, title, or date, you can swap this value.wp_reset_postdata(): Always call this after each loop to reset the global post object—this prevents weird behavior with subsequent queries on the page.
内容的提问来源于stack exchange,提问作者Tommy Adeniyi

