I want to create a search option for CPT using a plugin. Currently, the plugin displays data from the CPT posts using custom Gutenberg blocks. I am also able to display all post lists. I need to filter the post using the search form. The search options should search for categories and tags. Ideally, I want to display the search form using a Gutenberg block. What is the correct way to do it? I am new to plugin development. Plz advice.
1 Answer
Did you add your cpt to the search query ?
function include_cpt_in_search($query) {
if (is_admin() || !$query->is_main_query())
return;
if ($query->is_search) {
$query->set('post_type', array('post', 'page', 'your_custom_post_type'));
}
}
add_action('pre_get_posts', 'include_cpt_in_search');
-
I want to create the search as a Gutenberg Block ... Commented Jun 30, 2023 at 15:57