LearnDash: Keep Lessons and Quizzes Out of Site Search
Removes LearnDash lessons, topics, quizzes and certificates from your site's search results so visitors only find courses, not locked lesson pages.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
// Post types to hide from search.
$wpide_ld_hidden_types = array( 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz', 'sfwd-certificates', 'sfwd-question' );
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_ld_hidden_types ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return;
}
$types = $query->get( 'post_type' );
if ( ! $types || 'any' === $types ) {
$types = get_post_types( array( 'exclude_from_search' => false ) );
}
$query->set( 'post_type', array_values( array_diff( (array) $types, $wpide_ld_hidden_types ) ) );
} );
Notes
Courses stay searchable. Add sfwd-courses to the list to hide those too.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.