Created
April 5, 2023 02:05
-
-
Save everaldomatias/767262bc08efdd24cc0fe97459a23845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function custom_search_filter( $query ) { | |
global $wpdb; | |
if ( $query->is_search() && ! is_admin() ) { | |
$search_query = get_search_query(); | |
$results = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT p.ID, p.post_title, m.meta_value | |
FROM {$wpdb->prefix}posts AS p | |
INNER JOIN {$wpdb->prefix}postmeta AS m | |
ON p.ID = m.post_id | |
WHERE p.post_type = %s | |
AND p.post_status = 'publish' | |
AND m.meta_key = '_short_description' | |
AND m.meta_value LIKE %s", | |
'product', // Substitua 'product' pelo nome do seu tipo de post personalizado | |
'%' . $wpdb->esc_like( $search_query ) . '%' // Use a consulta de pesquisa padrão para obter o termo de pesquisa | |
) | |
); | |
$post_ids = array(); | |
foreach ( $results as $result ) { | |
$post_ids[] = $result->ID; | |
} | |
$query->set( 'post__in', $post_ids ); | |
} | |
} | |
add_action( 'pre_get_posts', 'custom_search_filter' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment