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.
- 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.
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.
- 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 edited Sep 24, 2019 at 11:22 Pete 1,0582 gold badges14 silver badges40 bronze badges asked Apr 29, 2013 at 21:47 Yaniv KossasYaniv Kossas 5892 gold badges6 silver badges11 bronze badges2 Answers
Reset to default 1This 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.
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745134244a4613124.html
评论列表(0条)