Advertisement
neatekFb

wp - get list of dates for posts with selected term

Mar 21st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  1. <?php
  2. // Vladimir Zhelnov // neatek.ru // neatek.pw
  3. // functions.php (Wordpress)
  4. function get_archive_date($date = '', $before = false, $term = 0) {
  5.     global $wpdb;
  6.     $res = array();
  7.     if(!empty(strtotime($date))) {
  8.         $date = date('Y-m-d H:i:s' ,strtotime($date));
  9.         if($before) {
  10.             $x = '<';
  11.         }
  12.         else {
  13.             $x = '>';
  14.         }
  15.         $sql = "SELECT year(post_date) FROM ".$wpdb->posts."";
  16.         if($term > 0) {
  17.             $sql .= " INNER JOIN ".$wpdb->term_relationships."
  18.                 ON ".$wpdb->posts.".ID = ".$wpdb->term_relationships.".object_id
  19.                 WHERE
  20.                 `term_taxonomy_id`='".$term."'";
  21.         }
  22.         $sql .= "AND `post_status`='publish'
  23.         AND `post_type` <>'attachment'
  24.         AND post_date ".$x." '".$date."'
  25.         GROUP by year(post_date)
  26.         ORDER by year(post_date) DESC";
  27.         $res = $wpdb->get_col($sql);
  28.     }
  29.     return $res;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement