0

We're working on a database with personal data from different sportsmen such as name, height, weight, etc.

We've managed to build a detailed filter through query_posts.

Every filter field is working as expected except for the text input field that we use to filter the sportsman by name. The problem is that when we are looking for the name of a single sportsman, the form just allows us to find the sportsmen by its exact name.

For example: If we are looking for a sportsman called John Doe, we would need to write the exact name of the post title ('John Doe'). Instead, we would like to be able to have results just typing 'John' or 'Doe'.

Our query_posts filter looks like this:

query_posts( array(
    'name' => $sportsman_name,
    'post_type' => 'db',
    'posts_per_page' => 100,
    'paged' => $paged,
    'category_name' => $sport,
    'meta_query' => array(
        $height,
        $weight,
        $tshirt_size,
        $foot_size,

    )
) );

That's how the form field looks like:

<input name="sportsman" type="text">

And that's how we create the string to filter the results in the query_posts:

$sportsman = $_GET["sportsman"];

    if($sportsman == ''){
        $sportsman_name = null;
    }else{
        $sportsman_name = $sportsman;
    }

2 Answers 2

1

Use search parameter 's' instead of 'name'

query_posts( array(
's' => $sportsman_name,
'post_type' => 'db',
'posts_per_page' => 100,
'paged' => $paged,
'category_name' => $sport,
'meta_query' => array(
    $height,
    $weight,
    $tshirt_size,
    $foot_size,
)));
0

First, use WP_query instead of query_posts.

$my_query = new WP_query( args... );
if ( $my_query->have_posts() ) {
    $my_query->the_post();
    $sportsman = ...
}

Second, to not filter by exact values you might use regular expressions (e.g., set the name within the array to "/^.*{$sportsman_name}.*$/"). Id did not test that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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