Hey there
As I only have a basic-indermediate knowledge about wordpress template structure maybe someone can help me out. How can I create a custom search results template with bricks for a custom post type only?
For Example I have a CPT Archive /help/ and would like to place a search form on the archive. I changed the form action URL to /help/ so the URL after search is /help/?s=whativesearchedfor. How can I display another template if it’s post type help and search?
I tried it multiple combinations of conditions like:
add_filter( 'bricks/active_templates', 'set_my_active_templates', 10, 3 );
function set_my_active_templates( $active_templates, $post_id, $content_type ) {
if ( ! bricks_is_frontend() ) {
return $active_templates;
}
if (is_post_type_archive('hp_help') && is_search()) {
$active_templates['archive'] = 4026;
}
return $active_templates;
}
Unfortunately this does nothing even though the conditions is_post_type_archive()
and is_search()
are correct.
I also tried a different approach with a custom search form and a hidden field to get another parameter for the search. /?s=&post_type=post
<input type="hidden" value="post" name="post_type" id="post_type" />
Unfortunately also in this case I couldn’t apply a custom search result template.
I would appreciate any help.
Update: I found a solution that works with the following code and setting a custom action url to the bricks search form with jQuery and a custom css id.
add_action('wp_head', function () {
if (is_post_type_archive('custom_post_type_slug')) {
?>
<script>
jQuery(document).ready(function() {
jQuery('#custom_search_form_id form').attr('action', '/custom_post_type_slug/');
})
</script>
<?php
}
});
add_filter( 'bricks/active_templates', 'set_my_active_templates', 10, 3 );
function set_my_active_templates( $active_templates, $post_id, $content_type ) {
//Only run my logic on the frontend
if ( ! bricks_is_frontend() ) {
return $active_templates;
}
$post_type = get_query_var('post_type', 'post');
if ($content_type = 'search' && $post_type == 'post_type_slug') {
$active_templates['search'] = 4026;
}
return $active_templates;
}