Relationship between post and category is not stored in the posts
table. You must extend your query for additional tables.
By category id
:
SELECT YEAR(p.post_date) FROM {$wpdb->posts} p
JOIN {$wpdb->term_relationships} tr ON tr.object_id = p.id
JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.term_id = 4 AND p.post_status="publish" AND p.post_type="post"
GROUP BY YEAR(p.post_date) DESC
Or by category slug
:
SELECT YEAR(p.post_date) FROM {$wpdb->posts} p
JOIN {$wpdb->term_relationships} tr ON tr.object_id = p.id
JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
WHERE t.slug = 'your_category_slug' AND p.post_type="post" AND p.post_status="publish"
GROUP BY YEAR(p.post_date) DESC
Related Posts:
- Main loop querying current template’s info only in custom category archive pages, not my posts
- How to make an activities stream mixing posts and comments?
- SQL query equivalent to WP User Query
- how to retrieve specific product attribute value in an sql query?
- WP_query category__in not working, only pulls from first category
- Get image of latest post from taxonomies/categories
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to correctly pass values to wpdb->prepare()?
- Optional Meta Query
- Editing the default wordpress search
- WP Query related posts by tags
- Order by summing multiple values
- Query posts intersecting tags and categories
- Check if loop has any categories?
- Filtering out child category posts from parent category archive not working
- Include data from custom table in WP_Query
- Is it possible to query from external database? [duplicate]
- Query where ANDing slug values not working
- How to SQL query posts IDs by categories AND authors?
- Post incorrectly excluded when using “category__in”?
- `offset` WP_Query argument dont work via `pre_get_posts`
- Filter products on category AND tag
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- Adding Category in WP_Query Not Working
- Show full category tree for a year with all post titles?
- Advanced Search – Is this possible?
- Block Editor – WordPress 6.1 – CPT Archive Issue – While the title changes in the loop, all records display the same data
- How to get latest posts from each category in regular order
- date_query seems to be ignored by wp_query
- wordpress ajax relationship query
- Is there a reason why Pages are not publicly_queryable?
- Query Page Content From Theme Options?
- Inserting HTML to close and open divs in WP_Query loops
- Check return value of get_posts
- Limiting number of related posts
- Better wordpress attachment query than this
- Exclude some authors from query
- Category ‘pad_counts’ & ‘parent’ conflict
- How to pass orderby params to $wpdb->prepare()?
- Sort WordPress Posts Meta value by Week not Day
- Query only the posts with a post format of “audio”
- Modify Search Query if original Query gave no results
- Is_single() conditional tag returns null in query
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Include current post into loop
- Debugging wp_query orderby for taxonomy
- Finding WordPress Posts assigned to multiple categories
- Can’t sort custom post type by random
- Include posts from some categories while excluding from others
- Include post content of linked posts in search
- pre_get_posts – Trying to get property of non-object warning
- Make a SQL query with wpdb in WordPress
- Query posts that contain video?
- Get multiple users with meta value in one query and populate WP_User class
- Getting additional columns from sql
- WordPress query posts with multiple post_meta data
- Combine results of multiple WP_Query to resemble single WP_Query
- List of ways to access WordPress database?
- Filter post query to only show direct children of category
- Get a list of posts by specific category
- Show a 404 error page if Public query variable’s value doesn’t exist?
- WordPress Custom Search Form Displaying Unexpected Results
- How to check the array values, what WP_Query has brought to me?
- Custom Post Type Query issue
- Overriding default calendar to show posts from a category
- Display if author page is author page of current user
- How to set up hierarchical relationships without using plugins / meta query
- How to add custom meta to ‘pre_get_terms’?
- Set front_page programatically after user login via query, while leaving site option alone
- Getting rid of unwanted nonSQL syntax characters when debugging a query
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Shortcode for custom query not returning results when attributes are added
- wp_query is showing posts from other categories
- Should I reset $wp_query?
- WP_Query Taxonomy categories filtering
- Yoast primary category query modification
- Filtering posts by archive showing all years
- Hide products in uncategorized category from search results
- Migrate posts from category and sub-category via SQL
- How to load a script code only in posts?
- How to filter query loop block with a search string from the query parameters
- Update query for wp_posts and wp_postmeta
- Slow Query On Search
- How to hide posts of a specific custom category in WordPress?
- How to echo woocommerce category name
- WP_Query: apply an SQL function to meta fileld value
- Get specific ACF key and value from all posts – no access to DB
- mysql query order by
- How to display a list of posts in same child category as current post
- Notice thrown when creating numeric pagination on custom query
- Slider won’t work with custom query
- wordpress udpate query not equal to
- WP_Query with one category in args shows other categories
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- How to exclude a category name from showing?
- Loop categories by recent post
- How order posts from category by date and comment count?