Include custom post type content in search

I created a custom post type (called 'produits') with the CPT UI plugin. I checked that both its parameters and Yoast SEO's allow custom post types to display in search results.

I tried to use several methods to include the custom post types in the global search results, such as this one and this one, I tried to use plugins too (such as Relevanssi and Search Everything) : nothing worked. In the following code though, I can echo the array content (the post types) correctly, so I suppose the problem is elsewhere :

function rc_add_cpts_to_search($query) {
    if( is_search() ) { 
        $post_types = get_post_types(array('public' = true, 'exclude_from_search' = false), 'objects');
        $searchable_types = array();
        if( $post_types ) {
            foreach( $post_types as $type) {
                $searchable_types[] = $type-name;
            }
        }
        $query-set( 'post_type', $searchable_types );
    }
    return $query;
}
add_action( 'pre_get_posts', 'rc_add_cpts_to_search' );

I'm not a developper, maybe I am overlooking something basic ? I don't really know where to search next, so... any help would be very welcome :)

I'm using WP 4.7.5, and a modified twenty-sixteen theme.

Edit for Doug As I used a plugin to generate the CPT, I'm not sure if the following code is what you need (I found it in the plugin page) :

function cptui_register_my_cpts_produits() {

/**
 * Post Type: Produits.
 */

$labels = array(
    "name" = __( 'Produits', '' ),
    "singular_name" = __( 'Produit', '' ),
    "menu_name" = __( 'Produits', '' ),
    "all_items" = __( 'Tous les produits', '' ),
    "add_new" = __( 'Créer un nouveau produit', '' ),
    "add_new_item" = __( 'Créer un nouveau produit', '' ),
    "edit_item" = __( 'Modifier un produits', '' ),
    "search_items" = __( 'Rechercher un produit', '' ),
    "not_found" = __( 'Aucun produit n'a été trouvé', '' ),
);

$args = array(
    "label" = __( 'Produits', '' ),
    "labels" = $labels,
    "description" = "Liste des produits Easy -Connect",
    "public" = true,
    "publicly_queryable" = true,
    "show_ui" = true,
    "show_in_rest" = false,
    "rest_base" = "",
    "has_archive" = false,
    "show_in_menu" = true,
    "exclude_from_search" = false,
    "capability_type" = "post",
    "map_meta_cap" = true,
    "hierarchical" = false,
    "rewrite" = array( "slug" = "produits", "with_front" = true ),
    "query_var" = true,
    "supports" = array( "title", "editor", "thumbnail", "custom-fields" ),
    "taxonomies" = array( "spot" ),
);

register_post_type( "produits", $args );
}

add_action( 'init', 'cptui_register_my_cpts_produits' );

UPDATE :

So, after days of tests, it seems that when I deactivate Polylang, custom post types do come up in the search results. I found this post, who seemed to have the same problem and solved it. But I can't apply the same correction : the plugin UI may have evolved since this post.

Topic theme-twenty-sixteen custom-post-types Wordpress search

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.