Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
share|improve this question
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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