Advertisement
Guest User

Retrieving all the categories and their posts from a taxonom

a guest
Jun 30th, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1. function widget( $args, $instance ) {
  2.         // Widget output
  3.         extract( $args );
  4.        
  5.         $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : null;
  6.         $level = sanitize_text_field( $instance['level'] );
  7.        
  8.         /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  9.         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  10.        
  11.         // Get all the taxonomies to which this object belongs
  12.         $type = get_post_type();
  13.         $customPostTaxonomies = get_object_taxonomies( $type );
  14.        
  15.         if(count($customPostTaxonomies))
  16.         {
  17.             echo $before_widget;
  18.            
  19.             if ( $title ) {
  20.                 echo $args['before_title'] . $title . $args['after_title'];
  21.             }
  22.            
  23.             foreach($customPostTaxonomies as $tax)
  24.             {
  25.                 // Get all the categories in the current taxonomy
  26.                 $args = array(
  27.                     'taxonomy' => $tax,
  28.                 );
  29.            
  30.                 $myPostTaxonomies = get_categories ( $args );
  31.        
  32.                 foreach($myPostTaxonomies as $myPostTax) {
  33.                     // Get all the posts in the current category
  34. ?>
  35.                     <<?php echo $level; ?>><?php echo $myPostTax->name; ?></<?php echo $level; ?>>
  36. <?php               $categoryArgs = array(
  37.                                 'orderby' => 'post_date',
  38.                                 'order' => 'DESC',
  39.                                 'post_type' => $type,
  40.                                 'category' => $myPostTax->slug // seems not to be working
  41.                                 );
  42.                    
  43.                     $postsArray = get_posts( $categoryArgs );
  44.                    
  45.                     if( count( $postsArray ) ) {
  46. ?>              <ul>   
  47. <?php                  
  48.                         foreach($postsArray as $myPost) {
  49. ?>
  50.                     <li><a href="<?php echo get_permalink( $myPost->post_title ); ?>"><?php echo $myPost->post_title; ?></a></li>
  51. <?php                   } ?>
  52.                 </ul>
  53. <?php               }
  54.                 }
  55.             }
  56.              
  57.             echo $after_title;
  58.         }
  59.        
  60.         echo $after_widget;
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement