1

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.

  • Could you post your CPT registration code, please? – Doug Belchamber Jun 1 '17 at 12:23
  • Hi Doug, I just edited the post. Hope this helps ! – Petipain Jun 1 '17 at 12:46
  • 1
    @Petipain you should use $query->is_search() rather than is_search(), else it will apply those changes to every query on the search page, not just the search queries, also there's a syntax issue with a single quote/apostrophe in the middle of a string in your registration code – Tom J Nowell Jun 1 '17 at 12:47
  • @TomJNowell I just corrected that, and fixed the syntax quote. Doesn't solve the search issue, but it sure is cleaner, thanks :) – Petipain Jun 1 '17 at 12:58
  • @Petipain it won't but if you're on a search page, that filter would change every query, such as related posts, widgets, footer stuff etc – Tom J Nowell Jun 1 '17 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.