The base content type structure in WordPress are posts. Pages, Comments, Attachments, etc., are posts in WordPress.
Some plugins add their own custom post types to take advantage of the base content structure. For example, WooCommerce adds the ‘product’ custom post type. LearnDash, adds the ‘sfwd-courses’ custom post type for courses. And so on.
These custom post types, by default, are enabled to be searched. When you do a search in WordPress, the WordPress search will bring all the matching posts within the custom post types enabled to be searched.
But sometimes you may want to prevent the WordPress search from including results from certain post types.
You can remove post types from the WordPress search feature by hooking into the register_post_type_args action in WordPress.
Post types are registered with a series of arguments, that set the properties they will have. Within the arguments, you can find one called ‘exclude_from_search’.
Keep in mind, you need to know the name of the custom post type you want to exclude from the search.
Let’s say, you want to exclude WooCommerce products.
A function that exclude products would be as follows:
To exclude multiple post types from the search, you can use a code like this:
/* LOOP POST TYPES */add_action('register_post_type_args','mws_loop_post_types',1,2);functionmws_loop_post_types($args,$post_type){$mws_post_types=array('sfwd-courses','sfwd-lessons');// Add the slug for as many post types as you want.// EXCLUDES FROM SEARCH ONLY THE SPECIFIC POST TYPES IN THE ARRAY mws_post_typesfor($i=0;$i<=(sizeof($mws_post_types));$i++){if(($mws_post_types[$i])==$post_type){$args['exclude_from_search']=false;}}return$args;}
Years ago, I was part of the “next shiny thing” tribe.
Something new popped up, that something I had to buy it.
A new Twitter method, a new ebook, a new software tool, whatever, on how to make money online.
That behavior was not healthy at all.
But thanks to that, I stumbled upon an idea that help me move towards what I want: “Do a business that you can tell your mom about”.
Well, I was a teenager and I don’t recall the exact same words. But the main idea stuck with me.
In businesses, and life in general, do something that you can tell your mom about. Whether you feel comfortable telling your mom you are a drug lord or a dentist, whatever it is, go for it.
There is really nothing you can’t do. Of course, always within your moral sense, ethic and the law.
You have a course set to Closed access mode. That means users will purchase the course either through a third-party eCommerce like WooCommerce, or a membership plugin like Paid Memberships Pro.
Sweet.
When a visitor clicks the course from the course list, they are taken to the single course page by default. Then, they click the ‘Take this course’ call to action to go to the purchase page.
Too many steps.
What if you want to cut the process to click the course > go to checkout?
What if you want to prevent visitors from seeing the course content before enrolling?
What if you want both?
You need to redirect the user to the value you set in the Button URL field in your course access mode setting.
But how?
Interestingly, the solution is straightforward. The steps are as follows
Hook into the template_redirect action.
Check if the current post is a course.
Check if the current user is admin. //admin
Check if the current user is not enrolled in the current course.
The template_redirect action is fired before determining which template will be loaded. This is perfect to decide whether to allow the single course page to load, or redirect the user to our specific destination.
Create a function that will hold all the logic for this feature. I’ll call it obi_closed_course_reditect().
functionobi_closed_course_reditect(){//Code will go here}
Then hook the function into the template_redirect action:
Good. Your function is now hooked into the template_redirect action. As it doesn’t have any logic yet, nothing will happen.
2 – Check if the current post is a course.
Courses in LearnDash are defined with the ‘swfd-courses’ post type. The get_post_type() function allows you to retrieve the current post’s post type.
Set a conditional to check if the post is not a course. If true, return false.
if('sfwd-courses'!=get_post_type()){return}
3 – Check if the current user is admin.
You are only interested in redirecting a user if they are non-logged-in visitors or regular users on your website but have not enrolled in your course yet.
You can achieve this by checking the user by their current capabilities. A unique capability for admins is ‘manage_options’. Then, filter users out like this:
if (current_user_can('manage_options'){
return
}
if(current_user_can('manage_options'){return}
4 – Check if the current user is not enrolled in the current course.
Give a current enrollee access to the single course page. LearnDash provides a function to check the current user enrollment status: ld_course_check_user_access().
5 – Retrieve the course options (serialized array).
The course options are stored in the database as metadata of the course post. The relevant meta key is ‘_sfwd-courses’. Use the get_post_meta() function to retrieve the course options and store them in a variable.
The course options are stored in the $courseOptionsSerialized variable as a serialized array.
You can access any option within the array by key. The course access mode is stored in the database as course price type. You can get this value like this