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