Skip to content

Instantly share code, notes, and snippets.

@DumahX
Last active May 6, 2024 10:08
Show Gist options
  • Save DumahX/527974caeb8ccc09e2569111320b087d to your computer and use it in GitHub Desktop.
Save DumahX/527974caeb8ccc09e2569111320b087d to your computer and use it in GitHub Desktop.
Displays a list of all published MemberPress courses.
<?php
function mpcs_all_courses($atts) {
$content = '';
$courses = get_posts(
array(
'post_type' => 'mpcs-course',
'post_status' => 'publish',
'posts_per_page' => '-1',
'orderby' => 'title',
'order' => 'ASC'
)
);
if(is_array($courses)) {
$content .= '<ul>';
if(has_post_thumbnail($course->ID)) {
$content .= get_the_post_thumbnail($course->ID, 'thumbnail');
$content .= '<br>';
}
foreach($courses as $course) {
$content .= '<li>';
$content .= '<a href="' . get_permalink($course->ID) . '">';
$content .= $course->post_title;
$content .= '</a>';
$content .= '</li>';
}
$content .= '</ul>';
}
return $content;
}
add_shortcode('mpcs-all-courses', 'mpcs_all_courses');
@anisur2805
Copy link

@DumahX hey, thanks for sharing the code snippet but it arise warning in L 17, it shows undefined variable $course with attempt to read property "ID" on null. Also there are missing few others checking before showing any course.

Thanks btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment