0

I'm trying to exclude a couple of custom post types from search results. The following works for one post type:

// Exclude custom post types from search
add_action('init', 'cdxn_remove_custom_type_from_search', 99);
function cdxn_remove_custom_type_from_search() {
    global $wp_post_types;
    if (post_type_exists('videos')) {
        // exclude from search results
        $wp_post_types['videos']->exclude_from_search = true;
    } 
}

If I build on this the first type is excluded but the second is not:

// Exclude custom post types from search
add_action('init', 'cdxn_remove_custom_type_from_search', 99);
function cdxn_remove_custom_type_from_search() {
    global $wp_post_types;
    if (post_type_exists('videos')) {
        // exclude from search results
        $wp_post_types['videos']->exclude_from_search = true;
    } elseif (post_type_exists('coordinator')) {
        // exclude from search results
        $wp_post_types['coordinator']->exclude_from_search = true;
    }
}
1
  • this isn't a WordPress problem, it's a generic programming problem, a misunderstanding of how if and else statements work
    – Tom J Nowell
    Sep 1, 2023 at 14:01

2 Answers 2

0

This is because elseif doesn't work the way you think it does. You're trying to use it in a way that's closer to a switch statement or multiple if statement`

This:

    if (post_type_exists('videos')) {
        // exclude from search results
        $wp_post_types['videos']->exclude_from_search = true;
    } elseif (post_type_exists('coordinator')) {
        // exclude from search results
        $wp_post_types['coordinator']->exclude_from_search = true;
    }

Is the same as this:

    if (post_type_exists('videos')) { // if videos exists
        // exclude from search results
        $wp_post_types['videos']->exclude_from_search = true;
    } else { // if it does not exist
        if (post_type_exists('coordinator')) {
            // exclude from search results
            $wp_post_types['coordinator']->exclude_from_search = true;
        }
    }

else if just shortens things so that it's more readable and takes less time to type. It's called syntactic sugar.

If you want to check multiple things in a row, then you should use multiple if statements in a row:

if ( condition1 is true ) {
    // do a thing
}

if ( condition2 is true ) {
    // do a thing
}
0
// Exclude custom post types from search
add_action('init', 'cdxn_remove_custom_types_from_search', 99);
function cdxn_remove_custom_types_from_search() {
    global $wp_post_types;

    if (post_type_exists('videos')) {
        // exclude 'videos' from search results
        $wp_post_types['videos']->exclude_from_search = true;
    }

    if (post_type_exists('coordinator')) {
        // exclude 'coordinator' from search results
        $wp_post_types['coordinator']->exclude_from_search = true;
    }
}

This code is working, I have tried it in local with xampp, I created the 2 post types (videos, coordinator) and added 1 post in each one of them, when I searched them, it only showed the pages and articles.

Your code was not working because, you are using "elseif" which it creates a conditional structure where only one of the conditions will be evaluated, and the first one that is true will execute, skipping the rest.

I hope I was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.