LearnDash Course Grid Returns No Results? Show a Custom Message.

Affiliate Disclosure: We may be compensated if you use our links to make a purchase. We are extremely selective in who we partner with & only recommend products we believe in. Our affiliate relationships do not influence our recommendations.

When using LearnDash course grid parameters to customize the output of the grid, some users or instances might return no results. By default, when the course grid doesn’t show any courses, the area is left blank. But what if you want to display a custom message in place of your grid when no courses are available?

This article will show you how to do that.

Bonus Tip: We’ll even show you how to display a custom Elementor template for a course grid with no results. 🤯

Using a Custom Code Snippet

First, let’s quickly talk about the code we’ll be using to make this happen. It’s PHP code that uses a LearnDash hook.

Recommended ProductKinsta 2 months of free hosting

My preferred way to make changes like this is with the Code Snippets plugin. You could also add this code to the functions.php file of your child theme (if you’re using one), but I find Code Snippets to be easier to manage and less likely to bring your site down if you make a mistake.

  1. Install & activate “Code Snippets” from your WordPress plugins page
  2. Navigate to Snippets > Add New in the left sidebar
  3. Give your snippet a name. Something like “LearnDash Course Grid – No Results Message”
  4. Paste the code snippet into the Code area
  5. Choose the Only run on site front-end option
  6. Click the Save Changes and Activate button

The Code

Here’s the code you’re going to use. It uses a filter for the ld_course_list hook. First, it checks to see if any courses have been found in your course grid query. If it doesn’t find any, it updates the $output parameter to include a custom message.

/**
 * Example usage for ld_course_list filter. Display a custom message when the shortcode returns no results
 */
add_filter(
	'ld_course_list',
	function( $output, $atts, $filter ) {
		// If the ld-course-list-items div does not have items/courses to display, use a custom message as output.
		if ( preg_match( '@<div class="ld-course-list-items row">\s+</div>@', $output ) ) {
			$output = "<p>No courses found.</p>";
		}
		// Always return $output.
		return $output;
	},
	10,
	3
);

What you can customize

In the code above, the only part that you should touch is the part following $output =, inside the quotes. This is what will be displayed when no courses are found.

The example above will display, “No courses found.”

This code uses HTML, thus why you see the opening <p> and closing </p> tags. That just stands for “paragraph,” and it ensures your text inside of it will be styled properly.

If you know HTML, feel free to get creative here. Use multiple <div>s, write your own custom CSS, etc.

Use an Elementor Template

If you don’t know HTML but still want to do something creative or more elaborate in this space, you can use an Elementor template.

Recommended Product

Rapyd WordPress Hosting
Rapyd just launched the fastest BuddyBoss, LearnDash & WordPress LMS hosting on the planet. Get 25% OFF with their early bird discount and lock-in up to 2 years of discounted pricing. From the creators of BuddyBoss.

Get 25% OFF Rapyd Today →

  1. Create a new Elementor Section and save it in your templates
  2. Go to Templates > Saved Templates
  3. Copy the Elementor Shortcode value

Elementor saved template shortcode

  1. Replace the $output = line above with this, using your unique template ID

$output = apply_shortcodes( '' );

Now when no courses are available in your grid, your custom Elementor template will be used.


Please share in the comments if you create something fun 😀.

Previous

Design Upgrade Compatibility with the Elementor for LearnDash Add-On

Next

How to Change the “Leave a Comment” Title in Focus Mode

9 Comments

  1. Jon

    Hello!

    Thanks for putting this out there however both versions of code did not work for me. I tried an Elementor Template and just the text version and nothing came back, just empty space. I don’t know how to code, but could it have something to do with the shortcode fields? (e.g. [ld_course_list progress_bar=”true”])

    Thanks for your help!

    • Hi Jon. Did you use the code in the purple box? Where did you put this code? It needs to be added as a new snippet in the Code Snippets plugin. Is that where you put it?

      That shortcode is structured properly so it’s not an issue with that. Although, that shortcode doesn’t limit which courses are shown, so it will show all of your courses—which means, as long as you have 1 course, the “no results” message will not be shown (because there are results).

  2. Jon

    Hello again,

    Sorry I made a mistake! I was using Code Snippets but it was in safe mode, hence not executing the code. After trying again, both variations of the code worked perfectly. It’s a really handy piece of customisation! Thank you!

  3. Very cool and also very useful. It would be neat to create a form within the Elementor template that prompts the user to “submit a course request” to collect feedback on what visitors are looking for, along with a list of categories/tags to guide them to the existing courses, as opposed to a blank no results page.

  4. Halil

    Hi Dave,

    When I read the post, I thought “this is the thing I’m looking for”, although I did everything like you described, this snippet doesnt work. It doesnt show the output message. Could you pls check the code ?
    thanks

    • I just tested it with the latest version of LearnDash, 4.3.1.2, and it works. You have to be using the [ld_course_list] shortcode. I don’t think it’ll work with the new version of the grid, or when using any blocks.

  5. Halil

    huge thanks Dave, u r so helpful.

  6. Mich

    Do you have an updated version of this code that would work with the Gutenberg block and course grid v2? I’m not enough of a coder to be able to tweak this myself.

    • Hi Mich. Unfortunately, I do not. And to be honest, I’m not even sure if it’s possible.

      I set up a course grid block (using v2) and used a query that showed no results. LearnDash doesn’t even display a message at all when there are no results. They just include an empty <div>. So I don’t think there are any hooks you could use to update the message because there is no message.

      Your best bet is to ask LearnDash support about this. Maybe you can convince them to update the Course Grid plugin and add a “No results.” message.

Leave a Reply

Your email address will not be published. Required fields are marked *