1

I have a custom post type that I would like to be searchable (I use the relevanssi search plugin). I have the following code to add the custom post type:

add_action('init', 'eyewebz_create_post_types');
function eyewebz_create_post_types() {
    register_post_type('project', 
        array(  'taxonomies'            => array('category'),
                'labels'                => $labels,
                'has_archive'           => true,
                'show_in_rest'          => true,
                'hierarchical'          => true,
                'public'                => true,
                'publicly_queryable'    => true,
                'supports'              => array('title', 'editor', 'post-formats', 'page-attributes', 'thumbnail', 'excerpt'),
                'menu_icon'             => 'dashicons-index-card'
    ));

}

This post type would need to show up in the search results, but I can't seem to be able to do that. I have tried altering the pre_get_posts as shown below, but I still don't get the results I need.

add_filter('pre_get_posts', 'extend_search_cpt');
function extend_search_cpt($query) 
{
    if($query->is_search) {
        $query->set('post_type', array('post', 'page', 'project'));
    }

    return $query; 
}

If I var_dump the query after I have set the post_type, I can see that it is included in the query, but I stil don't get any custom post types in my search result. (If I var_dump the query before, there is no sign of post_type in the query).

Any ideas what might be the issue?

2
  • Can you try 2 things? Put a namespace on your posttype to see if it shows up. It shouldn't need relevanssi to show in results as you have public set to true which defaults exclude_from_search to false. Try disabling it to see. I guess you could even try adding exclude_from_search to false just in case.
    – rudtek
    Commented Jan 21, 2019 at 16:16
  • Neither namespacing nor setting exclude_from_search did anything, unfortunately. It's the first time this doesn't work. I don't have any plugins running except for relevanssi, so no interference there either, I would think. Commented Jan 21, 2019 at 18:10

1 Answer 1

0

Please add exclude_from_search to false, inside your register post type function


add_action('init', 'eyewebz_create_post_types');
function eyewebz_create_post_types() {
    register_post_type('project', 
        array(  'taxonomies'            => array('category'),
                'labels'                => $labels,
                'has_archive'           => true,
                'show_in_rest'          => true,
                'hierarchical'          => true,
                'public'                => true,
                'publicly_queryable'    => true,
                'supports'              => array('title', 'editor', 'post-formats', 
                'page-attributes', 'thumbnail', 'excerpt'),
                'exclude_from_search' => false,
                'menu_icon'             => 'dashicons-index-card'
    ));

}

2
  • Doesn't work, unfortunately :) Commented Jan 22, 2019 at 20:12
  • it does not work Commented Jan 20 at 11:45

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.