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 a costume search in a site i am making.

When a user search a certain term, i want to display category list with a count of how many posts are found with in that category.

I thought of a way but i have problems doing it.

  1. in search.php in the loop in have_posts(), i will add a line that check each post's category and create an array with those categories ... and just +1 each time a certain category repeat itself...... problem with that since pagination is in place, it will only find the categories for the current page while the remaining pages stay unaccounted for.

The fix for that is having another wp_query that just use the s term. without the pagination.

Problem with that is i dont know how to query wp for a certain term like a normal search would do. i though of using query_posts somehow but still unclear on how.

Thanks in advanced.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

This is what i did:

I run another query without pagination like this:

$newQuaryVars = '&posts_per_page=99999999999999&post_type=post';
$posts = query_posts($query_string .$newQuaryVars);
$categoriesList = array();
$categoriesAmounts = array();

Then in the while have posts i did the following:

while ( have_posts() ) : the_post();
    $postCategories = get_the_category();
    if($postCategories){
        foreach($postCategories as $categoryInfo) {
            $categoriesAmounts[$categoryInfo->term_id]['amount']++;
            $categoriesList[$categoryInfo->term_id] = array( 
                'name' => $categoryInfo->name, 
                'url' => get_category_link( $categoryInfo->term_id ), 
                'amount' => $categoriesAmounts[$categoryInfo->term_id]['amount']
            );
        }
    }
endwhile;

Now i basically have an array with categories name / url and the amount of posts that fit the search term within the that category.

after that i just array_multisort it and displayed it.

Hope that helps anyone out there.

share|improve this answer
    
Quadrillion, really?? :) That's a lot of posts! You might find posts_per_page = -1 or nopagination = true query parameters useful if you want to disable pagination altogether! –  Michal Mau Apr 30 '13 at 7:50
    
Also plase don't use query_posts. get_posts will serve you better here - see wordpress.stackexchange.com/questions/1753/… Anyway - congrats on taming your query and welcome on WPSE! :) –  Michal Mau Apr 30 '13 at 8:03
    
Thanks you both michal mau, thanks for tips i've checked the link thanks. –  Yaniv Kossas Apr 30 '13 at 19:55
add comment

It is easy to get the number of posts in a category...

$qry = new WP_Query(array('cat'=>2));
var_dump($qry->found_posts);

... but you could very easily have a lot of queries that way. Maybe something like this would help reduce that:

$catid = 1;
$qry = new WP_Query;
$cat_count = array();
if (!isset($cat_count['cat-'.$catid])) {
    $qry->query(array('cat'=>$catid));
    $cat_count['cat-'.$catid] = $qry->found_posts;
}
var_dump($cat_count)

At least it only runs a query the first time you encounter a category.

share|improve this answer
    
Nah you either missed what i am trying to achieve or thinking about a different idea.. my idea was lets say a search for "blue cars" is being preformed. i have car companies set as category, E.G dodge, ford, toyota..... all those companies have blue cars hence all 3 will come in the results. Now what i wanted is to show a list of all categories that has a post(in my example a blue car) and to count them. I found out how to do it will post an an answer in a sec. –  Yaniv Kossas Apr 30 '13 at 2:15
    
"i want to display category list with a count of how many posts are found with in that category"-- Sorry, I don't see the different between that and what I just did, but glad you got it solved. If you want to further restrict the results just pass in the same values as used in the original query. –  s_ha_dum Apr 30 '13 at 2:38
    
I've posted my answer if you like to take a look. –  Yaniv Kossas Apr 30 '13 at 3:56
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.