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 |1 Answer
Reset to default 0Please 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
版权声明:本文标题:Custom post type not showing in search result 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288952a1928202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
public
set to true which defaultsexclude_from_search
to false. Try disabling it to see. I guess you could even try addingexclude_from_search
to false just in case. – rudtek Commented Jan 21, 2019 at 16:16