admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Jan 21, 2019 at 12:43 Michiel StandaertMichiel Standaert 3733 gold badges5 silver badges17 bronze badges 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. – Michiel Standaert Commented Jan 21, 2019 at 18:10
Add a comment  | 

1 Answer 1

Reset to default 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'
    ));

}

本文标签: Custom post type not showing in search result