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.

I have form where I list all terms associated with posts as check boxes, user should pick few of them, hit search and get all posts that match all chosen terms. It works great when there are posts that match search criteria. My question is how do I display posts that have just few of matched terms and how to sort results based on number of matches. Any help is much appreciated. Thanx.

So this is the form inside search page:

<form action="<?php bloginfo('template_url') ?>/build_search.php" method="post" accept-charset="utf-8">
<?php $terms = get_terms( 'my_tax' );
$checkboxes = '';
foreach($terms as $term) :
    $checkboxes .='<input type="checkbox" name="term[]" value="'.$term -> slug.'" id="term-'.$term->term_id.'" /><label for="term-'.$term->term_id.'">'.$term->name.'</label>';
endforeach;
print $checkboxes;
?>
<input id="submit" type="submit" value="Search">

This is what I put in build_search.php file

if (isset($_POST["term"])){ $terms_array = array(); $terms_array = $_POST["term"]; foreach ($terms_array as $key => $value) { $string .= $value.'+'; } $terms_string = substr($string, 0, -1); $term = $terms_string; } else { $term = ""; } $url = header("Location:/?my_tax=$term");

after that I have only taxonomy-my_tax.php with regular loop in it.

share|improve this question
    
whats your search query/filter like? –  Bainternet Apr 16 '11 at 0:57
    
Search output this to (Location:/?my_tax=cat1+cat2+cat3+cat4) After that it is displayed by taxonomy-my_tax.php. I need a way to show all posts from cat1, cat2, cat3, cat4 and order them depending on how much categories matched. –  Bezbeli Apr 16 '11 at 12:59
    
Are you sure that your search results are displayed by taxonomy-my_tax.php? –  Bainternet Apr 16 '11 at 15:22
    
Yes, and everything works fine, I get posts that match ALL the terms, now I need to get all posts that match ANY of the terms and to order them by number of matched terms –  Bezbeli Apr 16 '11 at 15:30
add comment

1 Answer

Well the only way i can think of is to create an two dimentions array of the results you want to output, and the number of matching tags.

so for example:

$results = array();
$searched_tags = $_post['my_tax'];
$searched_tags = explode("+", $searched_tags);
while (have_posts()){
    $the_post();
    $result['r'] = '<div class="post">
    <div class="title"><h2><a href="'.get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">'.get_the_title($post->ID).'</a></h2></div>
    <div class="excerpt">'.get_the_excerpt().'</div>
    </div>';
    //get current posts terms of the taxonomy
    $current_post_terms = wp_get_object_terms( $post->ID, 'my_tax', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'names'));
    $matchs = 0;
    //check and count matchs
    foreach ($current_post_terms as $t){
        if (in_array($t,$searched_tags){
            $matchs = $matchs + 1;
        }
    }
    $result['m'] = $matchs;
    //save results to array
    $results[] = $result;
}
//then sort array by matchs count
//quick sorting function
function cmp($a, $b) {
    if ($a['m'] == $b['m']) {
        return 0;
    }
    return ($a['m'] > $b['m']) ? -1 : 1;
}
//the actuall array sort
uasort($results, 'cmp');
foreach ($results as $result){
    echo $result['r'];
}

this is untested so try and report back.

share|improve this answer
    
Thanx for reply, there was an error in //check and count matchs foreach ($current_post_terms as $t){ if (in_array($t,$searched_tags){ $matchs = $matchs + 1; } } $result['m'] = $matchs; I corrected it but it still doesn't work, I should put this code inside the taxonomy-my_tax.php right? –  Bezbeli Apr 16 '11 at 20:38
    
Its an example!!! I'd have to see the full code of the search form and taxonomy file to know better –  Bainternet Apr 16 '11 at 21:05
    
Sorry, I should add code from the start. –  Bezbeli Apr 16 '11 at 22:06
    
seeing your code i think i know whats the problem, change $searched_tags = $_post['my_tax']; to $searched_tags = $_GET['my_tax']; and yes the whole code needs to go in your taxonomy-my_tax.php –  Bainternet Apr 16 '11 at 22:36
    
Yes that was it, now I get the selected terms when I echo $searched_tags but then it breaks on while loop. –  Bezbeli Apr 16 '11 at 23:13
show 34 more comments

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.