• sacconi

    (@sacconi)


    This function doesn work well

    // --- RICERCA PER NOME su custom fields (front-end e admin) ---
    function custom_search_query($query) {
    if ($query->is_search && isset($query->query_vars['s']) && !empty($query->query_vars['s'])) {
    $searchterm = $query->query_vars['s'];
    $custom_fields = array('function_name'); // campi meta da cercare
    $meta_query = array('relation' => 'OR');

    foreach ($custom_fields as $cf) {
    $meta_query[] = array(
    'key' => $cf,
    'value' => $searchterm,
    'compare' => 'LIKE'
    );
    }

    // Disabilita ricerca standard testo
    $query->set('s', '');
    $query->set('meta_query', $meta_query);
    }
    }
    add_action('pre_get_posts', 'custom_search_query');

    Imagine I have an apartment (post type=post) which has for name (meta= function_name): 6805 Martina, well, if I write “martina”, or “mar” or “ma”, I get result(s), if I write 6805, I get nothing. Consider that I have another apartment “Sunbeach B5”, if I write B5 it works, I get result(s), how can I fix it?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Mustafa Bharmal

    (@mustafabharmal)

    Because LIKE in MySQL is case-insensitive but string-based, searching “6805” fails if WordPress sanitizes s as a string and skips numeric-only values.

    Solution: cast search term to string and force a meta query even for numbers, add this before $query->set(‘s’, ”);:

    $searchterm = strval($query->query_vars['s']);

    Also, add ‘type’ => ‘CHAR’ in your meta query:

    $meta_query[] = array(
    'key' => $cf,
    'value' => $searchterm,
    'compare' => 'LIKE',
    'type' => 'CHAR',
    );

    This makes numeric searches like “6805” work.

    Thread Starter sacconi

    (@sacconi)

    sorry but it doesnt work, maybe it should be coordinated with the previous function? I write both of them

    // --- CERCA PER ID (front-end e admin) ---
    function search_by_post_id($query) {
    // Controlla solo query di ricerca
    if ($query->is_search && isset($query->query_vars['s']) && is_numeric($query->query_vars['s'])) {
    $query->set('post_type', 'post'); // limita ai post
    $query->set('post__in', array((int) $query->query_vars['s'])); // cerca per ID
    $query->set('s', ''); // disabilita ricerca testo standard
    }
    }
    add_action('pre_get_posts', 'search_by_post_id');


    // --- RICERCA PER NOME su custom fields (front-end e admin) ---
    // --- RICERCA PER NOME su custom fields (front-end e admin) ---
    function custom_search_query($query) {
    if ($query->is_search && isset($query->query_vars['s']) && !empty($query->query_vars['s'])) {
    $searchterm = strval($query->query_vars['s']);
    $custom_fields = array('function_name'); // campi meta da cercare
    $meta_query = array('relation' => 'OR');

    foreach ($custom_fields as $cf) {
    $meta_query[] = array(
    'key' => $cf,
    'value' => $searchterm,
    'compare' => 'LIKE',
    'type' => 'CHAR',
    );
    }

    // Disabilita ricerca standard testo
    $query->set('s', '');
    $query->set('meta_query', $meta_query);
    }
    }
    add_action('pre_get_posts', 'custom_search_query');
    Moderator bcworkz

    (@bcworkz)

    When you enter only a number like “6805”, it triggers the post ID search. When your intention is to search for an address like “6805 Martina”, the functions are still going to try to find a post ID of 6805, not an address. The result will be either nothing or the post with ID 6805, which is very unlikely to be the correct post with a similar address.

    There’s no way for WP to know if you want an address or an ID with only “6805” search term to go on. You could either alter the form with another field to indicate what kind of search to perform; or have separate search forms; or always do two separate searches and merge the results. Merged results could be confusing since some of the results will be irrelevant, but what you are seeking will be in the results somewhere.

    Thread Starter sacconi

    (@sacconi)

    ok, maybe could I create a custom search box in my post table page? So that different searches start from different boxes?

    Thread Starter sacconi

    (@sacconi)

    good advice, I found a solution using a custom search box in the post table + modyifing the function

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.