0

I need to perform partial searches of wp_usermeta, I have the following code to do the search for wp_usermeta, but I need you to do partial searches.

<form id="filtros" method="GET">
     <label for="poblacion">Poblacion:</label> <input type="text" name="poblacion">
     <label for="trabajo">Trabajos:</label> <input type="text" name="trabajo">
     <input type="submit">
</form>

<?php
$args = array( 
    'role' => 'empresa-BBDD',
    'meta_key' => 'poblacion',
    'meta_value' => $_GET['poblacion'],
    'meta_key' => 'categorias',
    'meta_value' => $_GET['trabajo'],
    'orderby' => 'nicename',
    'order' => 'ASC',
);

$usuarios = get_users( $args );

foreach ($usuarios as $usuario) {
    echo '<div class="empresa-bbdd">'; 
    echo '<h4><a href="/perfil-empresa/?id='.$usuario->ID.'">' . $usuario->user_firstname . '</a></h4>';
    echo '<p class="dir-emp"><i class="fa fa-map-marker" aria-hidden="true"></i> ' . $usuario->calle . ', ' . $usuario->cp . ', ' . $usuario->poblacion . '</p>';
    $categorias = $usuario->categorias;
    $replace = array('[', ']', '"');
    echo '<p class="cat-emp"><b>Trabajos que realiza:</b> '. (str_replace($replace," ",$categorias)) .'</p>';
    echo '</div>';
}
1
  • Never use unsanitized data which can be counterfeited by the user. For example, $_GET in your case. – Max Yudin May 16 '17 at 19:13
0
<?php
// ALWAYS sanitize user input!
$poblacion = sanitize_text_field( $_GET['poblacion'] );
$trabajo = sanitize_text_field( $_GET['trabajo'] );

$usuarios = get_users(
    array(
        'role' => 'empresa-BBDD',
        'order_by' => 'nicename',
        'order' => 'ASC',
        'meta_query' => array( // search for user meta data
            'relation' => 'AND', 
            array(
                'key'     => 'poblacion',
                'value'   => $poblacion,
                'compare' => 'LIKE' // partial comparison
            ),
            array(
                'key'     => 'trabajo',
                'value'   => $trabajo,
                'compare' => 'LIKE' // partial comparison
            )
        )
    )
);

'relation' => 'AND' is used to find users with both keys matching. Otherwise, use 'OR'.

'compare' => 'LIKE' is the same as '%LIKE%' in MySQL, which is to say using 'wildcards' on both ends. Search for '34' will give you '1234', '2345' and '34567'.

Note! The code was not tested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.