• 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 6 replies - 1 through 6 (of 6 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

    The issue happens because two different pre_get_posts functions are running. When the search term is numeric (like 6805), the search_by_post_id function takes priority and the meta field query never runs, which is why “6805 Martina” cannot be found.

    A better approach is to handle both conditions inside a single function so the query can check the post ID and the custom field together.

    Example:

    function custom_combined_search($query) {
        if (!is_admin() && $query->is_main_query() && $query->is_search()) {
    
            $searchterm = $query->get('s');
    
            $meta_query = array(
                array(
                    'key'     => 'function_name',
                    'value'   => $searchterm,
                    'compare' => 'LIKE'
                )
            );
    
            $query->set('meta_query', $meta_query);
    
            if (is_numeric($searchterm)) {
                $query->set('post__in', array((int)$searchterm));
            }
        }
    }
    add_action('pre_get_posts', 'custom_combined_search');
    

    This way numeric searches like “6805” can match both the post ID and the custom field value “6805 Martina”.

    I had to deal with similar numeric input behavior while building small web tools, where user input can be numbers or text. I recently worked on making one of my projects more responsive and user-friendly for numeric inputs as well: https://ricepuritytesthub.com/

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

You must be logged in to reply to this topic.