Quick fix to add post_type to search form widget in Elementor

Add post_type|product in attributes of your widget and this code snippet in your functions.php:

<?php

// Add post_type to search form
function add_post_type_to_search_form( $widget_content, $widget ) {

if ( 'search-form' === $widget->get_name() ) {
$settings = $widget->get_settings();

if ( strpos($settings['_attributes'],'post_type|product') !== false ) {
$widget_content = str_replace('</form>','<input type="hidden" name="post_type" value="product" /></form>', $widget_content);
}
}

return $widget_content;
}
add_filter( 'elementor/widget/render_content', 'add_post_type_to_search_form', 10, 2 );

Leave a Comment