Help Me Understand How To Get Jetpack Search to Search a Custom Post Type

UPDATE: I’m just gonna put the answer at the top of this blog post to help anyone finding this. Do these things:

  1. Ensure the CPT is registered with:
"public" => true,
"publicly_queryable" => true,
"exclude_from_search" => false,Code language: PHP (php)
  1. Ensure the CPT is allowed in the REST API:
function allow_my_post_types($allowed_post_types) {
  $allowed_post_types[] = "YOUR_CPT_SLUG";
  return $allowed_post_types;
}
add_filter("rest_api_allowed_post_types", "allow_my_post_types");Code language: PHP (php)
  1. Ensure you’re not filtering it out inadvertently in a jetpack_instant_search_options hook.
  2. Ensure you’re not filtering it out inadvertently in the Jetpack Search “Customize” settings

Once all that is in place, doing a manual sync is in order at https://wordpress.com/settings/manage-connection/{YOUR_DOMAIN}

And now the original post…

I’ve got a Custom Post Type in WordPress. It’s called docs because it’s for documentation pages.

register_post_type("docs", [
    "labels" => [
        "name" => __("Docs"),
        "singular_name" => __("Docs Page"),
        "add_new" => __("Add Docs Page"),
        "add_new_item" => __("Add New Docs Page"),
        "edit_item" => __("Edit Docs Page"),
    ],
    "public" => true,
    "publicly_queryable" => true,
    'exclude_from_search' => false,
    "has_archive" => true,
    "rewrite" => ["slug" => "docs"],
    "show_in_rest" => true,
    "hierarchical" => true,
    "supports" => [
        "title",
        "editor",
        "thumbnail",
        "excerpt",
        "page-attributes",
        "revisions",
    ],
]);Code language: PHP (php)

This is for the CodePen 2.0 Docs.

The Classic Docs are just “Pages” in WordPress, and that works fine, but I thought I’d do the correct WordPress thing and make a unique kind of content a Custom Post Type. This works quite nicely, except that they don’t turn up at all in Jetpack Search.

I like Jetpack Search. It works well. It’s got a nice UI. You basically turn it on and forget about it. I put it on CSS-Tricks, and they still use it there. I put it on the Frontend Masters blog. It’s here on this blog. It’s a paid product, and I pay for it and use it because it’s good. I don’t begrudge core WordPress for not having better search, because raw MySQL search just isn’t very good. Jetpack Search uses Elasticsearch, a product better-suited for full-blown site search. That’s not a server requirement they could reasonably bake into core.

But the fact that it just doesn’t index Custom Post Types is baffling to me. I suspect it’s just something I’m doing wrong.

I can tell it doesn’t work with basic tests. For example, I’ve got a page called “Inline Block Processing” but if you search for “Inline Block Processing” it returns zero results.

In the Customizing Jetpack Search area, I’m specifically telling Jetpack Search not to exclude “Docs”. That very much feels like it will include it.

Screenshot of the CodePen blog search interface, displaying a search result for the term 'block' with highlighted entries, filtering options, and excluded post types settings.

I’ve tried manually reindexing a couple of times, both from SSHing into Pressable and using WP-CLI to reindex, and from the “Manage Connections” page on WordPress.com. No dice.

I contacted Jetpack Support, and they said:

Jetpack Search handles Custom Post Types individually, so it may be that the slug for your post type isn’t yet included in the Jetpack Search index.
 
We have a list of slugs we index here:
 
https://github.com/Automattic/jetpack/blob/trunk/projects/packages/sync/src/modules/class-search.php#L691
 
If the slug isn’t on the list, please submit an issue here so that our dev team can add it:

Where they sent me on GitHub was a bit confusing. It’s the end of a variable called private static $unindexed_postmeta, which doesn’t seem quite right, as that seems like, ya know, post metadata that shouldn’t be indexed, which isn’t what’s going on here. But it’s also right before a variable called private static $taxonomies_to_sync, which feels closer, but I know what a taxonomy is, and this isn’t that. A taxonomy is categories, tags, and stuff (you can make your own), but I’m not using any custom taxonomies here; I’m using a Custom Post Type.

They directed me to open a GitHub Issue, so I did that. But it’s sat untouched for a month.

I just need to know whether Jetpack Search can handle Custom Post Types. If it does, what am I doing wrong to make it not work? If it can’t, fine, I just wanna know so I can figure out some other way to handle this. Unsearchable docs are not tenable.

Thoughts? Email me or comment below. Also CodePen PRO is quite a deal. 🙏

4 responses to “Help Me Understand How To Get Jetpack Search to Search a Custom Post Type”

  1. Dang! I thought this would be simple and straight forward but apparently not.

    It sounds like they keep their list of allowed post types secret. Probably to avoid plugins from stuffing their ElasticSearch index with a bunch of junk.

    As a quick and dirty test try changing register_post_type("docs", [] ); to one of JetPack’s own custom post types, jetpack-portfolio, and see if it indexes. You’ll need to add the following the arguments for registering the custom post type:

    "rewrite" => [
    "slug" => "docs",
    "with_front" => false
    ],

    Everything user facing will still say Docs.

  2. Jay Hoffmann says:

    As far as I know Jetpack Search should work with any public post type by default. But the index can take a bit. I do wonder if this would help: https://specialprojects.automattic.com/project-handbook/using-cpts-with-jetpack-search/

    That being said, I have this suspicion that the problem is actually the slug of your post type being “docs.” There are exclusions for docs folders in other places and I wouldn’t be that surprised if it was somehow excluded from indexing. I couldn’t find anything in the code that would suggest that though. I wonder if changing the slug of the post type would help?

  3. Jeremy says:

    I’m sorry, it looks like the issue you opened fell through the cracks! I’ve just replied to it just now.

    The trick is the rest_api_allowed_post_types filter. Do not hesitate to reply to the issue if you have other questions, you won’t have to wait another month for a reply :)

  4. Chris Coyier says:

    Thank you everyone! I’m trying out a fix now and I’ll update the blog post with the answer at the top if it works.

Leave a Reply

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