Skip to content
Advertisement

Simulating SELECT YEAR(post_date) SQL query using only WP Query

I’m not entirely sure this is possible but I’m completely out of ideas. The task I’m working on requires that I specifically use WP_Query due to constraints of other efficiency plugins my organization uses.

Pure and simple, I need a way to simulate the following query using WP_Query:

"SELECT YEAR(post_date) FROM wp_posts WHERE post_status = 'publish' GROUP BY YEAR(post_date) DESC"

Before you answer, note again, using the $wpdb global is off the table, and I’ve attempted to employ the parse_query method from WP_Query but it doesn’t seem to respond to the above query in any meaningful way.

Thank you to anyone who takes the time to think this over!

Advertisement

Answer

Probably try this:

I’ve tried not to use $wpdb in 2 functions at the top as per your requirements.

let me know if this worked for you.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement