Search in WP, "%" does not work

0

I have a wordpress search and it uses the following:

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "%'.$wpdb->esc_like($this->table_search).'%"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

The problem is that he does not think that way, but if I put what I want to look like this:

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "%teste%"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

or

        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table . ' ucf WHERE name LIKE "'.$wpdb->esc_like($this->table_search).'"',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);

and search the full text without the % % he thinks, does anyone know why? I need a search that finds any part of the text and not only if the whole text is ...

    
asked by anonymous 24.10.2016 / 15:48

1 answer

1

I think the problem is not % , but the way you're using $wpdb->prepare() :

$like = '%' . $wpdb->esc_like($this->table_search) . '%';
$form_data = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM $table ucf WHERE name LIKE '%s'",
        $like
    )
, ARRAY_A);

In the prepare() method, the first parameter is the complete string with placeholders ( %s to string, %d to int, for example, the same as sprintf() ). The others are values that must be inserted in each placeholder, in order. In its example as it had no placeholder in the string and two subsequent values, something wrong was happening.

    
25.10.2016 / 02:00