Advertisement
jellybean_cooper

Date and Category Query

Sep 18th, 2015
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. <?php $categorys = get_terms( 'category', array( 'hide_empty' => true, 'fields' => 'all' ) ); ?>
  2. <form class="staff-filter" method="GET" action=""><div class="col-sm-12 text-center">
  3. <span>Filter Posts by:</span>
  4. <ul class="list-inline">
  5. <li>
  6. <label>
  7. <select name="category">
  8. <option value="" disabled selected> Category </option>
  9. <?php foreach( $categorys as $category ) : ?>
  10. <option value="<?php echo $category->slug; ?>">
  11. <?php echo $category->name; ?>
  12. </option>
  13. <?php endforeach; ?>
  14. </select>
  15. </label>
  16. </li>
  17.  
  18. <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
  19.  
  20. $months = $wpdb->get_col("SELECT DISTINCT MONTHNAME(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");?>
  21.  
  22. <li>
  23. <select name="date_start">
  24. <option value="" disabled selected> Date </option>
  25. <?php foreach($months as $month) : ?>
  26. <option> <?php echo '<ul><li class"list-unstyled"><a href="'. site_url() .'/'.$year .'/'.date('m', strtotime($month)).'"/> ' . $month .'</a></li></ul>';?></option>
  27. <?php endforeach; ?>
  28.  
  29. <?php foreach($years as $year) : ?>
  30. <option><?php echo '<ul><li class"list-unstyled"><a href="'. site_url() .''.$year.'"/> ' . $year .'</a></li></ul>';?></option>
  31. <?php endforeach; ?>
  32. </select>
  33. </li>
  34. </ul>
  35. <!-- SUBMIT BUTTON -->
  36. <button class="btn"><span class="glyphicon glyphicon-search" aria-hidden="true"></span> Search</button>
  37. <!-- END SUBMIT BUTTON -->
  38. </div>
  39. </form>
  40.  
  41. <?php
  42.  
  43. $cat_query = array(array('relation' => 'AND'));
  44.  
  45. if( isset( $_GET['category'] ) && $_GET['category'] ){
  46.  
  47. $cat_area_query = array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $_GET['category'],'operator' => 'IN', );
  48. $cat_query[] = $cat_area_query;
  49. }
  50.  
  51. if( $cat_query && $cat_query ){
  52. $cat_query['relation'] = 'AND'
  53. ;
  54. }
  55.  
  56. $args = array(
  57. 'post_type' => array('post'),
  58. 'post_status' => 'publish',
  59. 'tax_query' => $cat_query,
  60. 'orderby' => 'date',
  61. 'order' => 'desc',
  62. 'date_query' => array(
  63. 'relation' => 'OR',
  64. array(
  65. 'year' => $getdate['year'],
  66. 'month' => array(9, 8,7, 6, 5),
  67. 'compare' => '=',
  68. ),
  69. ),
  70. );
  71. $posts_query = new WP_Query( $args );?>
  72.  
  73. <!-- CONTAINER -->
  74.  
  75. <?php if( $posts_query->have_posts() ) : ?>
  76.  
  77. <?php while( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
  78.  
  79. <div class="ms-item col-lg-6 col-md-6 col-sm-6 col-xs-12">
  80.  
  81. <?php if (has_post_thumbnail()) : ?>
  82.  
  83. <figure class="article-preview-image">
  84.  
  85. <a href="<?php the_permalink(); ?>" class="post-title-link"><?php the_post_thumbnail(); ?></a>
  86.  
  87. </figure>
  88.  
  89. <?php else : ?>
  90.  
  91. <?php endif; ?>
  92. <a href="<?php the_permalink(); ?>">
  93. <div class="post-content white">
  94. <?php $category = get_the_category(); ?>
  95. <span class="post-category"><?php echo $category[0]->cat_name;?></span>
  96. <span class="post-date"><i class="fa fa-clock-o"></i> <?php the_time('F j, Y') ?></span>
  97. <h2 class="post-title"><?php the_title(); ?></h2>
  98.  
  99. <?php the_excerpt(); ?>
  100. </div>
  101. </a>
  102.  
  103. <?php endwhile;?>
  104. <?php endif;?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement