Advertisement
alchymyth

getting the right cats

Sep 24th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. $allposts = get_posts('post_type=post&numberposts=100'); //get a lot of recent posts
  2. $cats = array(); $cat = array(); //initialize arrays
  3. foreach($allposts as $allpost) {
  4. $postcats = get_the_category($allpost->ID); //get all post's categories
  5. foreach($postcats as $ca) { $cat[] = $ca->term_id; } $postcats = $cat; $cat = array(); //this uses less queries than wp_get_post_categories()
  6. $cats = array_merge($cats,$postcats);
  7. $cats = array_unique($cats); } //only keep unique category IDs
  8.  
  9. $allcats = get_categories(); //get all categories of the site, in case tha latest posts are not covering all categories/
  10. foreach($allcats as $allcat) { $sitecats[] = $allcat->term_id; }; //get the category IDs
  11.  
  12. $cats = array_merge($cats, $sitecats); //add to the already collected categories
  13. $cats = array_unique($cats); //only keep unique category IDs
  14.  
  15. $section_cats = array(1,23,48,128,9,19,67); //allowed categories for the automated section
  16.  
  17. $cats = array_intersect(  $cats, $section_cats );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement