# SELECT DISTINCT of a property

**URL:** <https://forum.pinegrow.com/t/select-distinct-of-a-property/4726>\
**Category:** Pinegrow WP\
**Created:** [November 28, 2020, 4:46am UTC](https://forum.pinegrow.com/t/select-distinct-of-a-property/4726 "2020-11-28T04:46:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoWin](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@HoWin](https://forum.pinegrow.com/u/HoWin)\
**Post date:** [November 28, 2020, 4:46am UTC](https://forum.pinegrow.com/t/select-distinct-of-a-property/4726/1 "2020-11-28T04:46:08Z")

</div>

I have a question for the WP experts 🙂

Is it possible with standard wordpress actions to make a SELECT DISTINCT query of a property (in my special case e.g. from year month from post\_date) and then save these values in an array?

I currently solved this problem for the post\_type ‘news’ via PHP as follows:

```
<?php
global $wpdb;

// 1.) find all years from post_date
$post_y = $wpdb->get_results("
    SELECT DISTINCT
      YEAR(p.post_date) as post_year
    FROM
      {$wpdb->posts} AS p
     WHERE (p.post_type ='news'
      AND p.post_status ='publish')
    ORDER BY p.post_date DESC
", OBJECT );

// outer Loop: post_year - START
foreach ( $post_y as $year ):
  $py = $year->post_year; // needed for filter main-loop

  // 2.) find all month within the given year
  $post_m = $wpdb->get_results("
      SELECT DISTINCT
        MONTH(p.post_date) as post_month
      FROM
        {$wpdb->posts} AS p
      WHERE (p.post_type ='news'
        AND p.post_status ='publish'
        AND YEAR(p.post_date) = $py)
      ORDER BY p.post_date DESC
  ", OBJECT );

  // inner Loop: post_month within post_year - START
  foreach ( $post_m as $month ):
    $pm = $month->post_month; // needed for filter main-loop
    $pmy = $py . "-" . $pm . "-01"; // needed for h1 | Title
    $Title = date('F Y', strtotime($pmy));
    
          // MAIN LOOP goes here using $pm and $py as filter

  // inner Loop: post_month within post_year - END
    endforeach;

// outer Loop: post_year - END
endforeach;

?>

```

That works, but I would be interested if you can get the PG WP Actions to do it and I just haven’t found it yet.

Best regards and happy coding 🙂  
Horst
