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?
- Using OR conditions in meta_query for query_posts argument
- What is the most efficient way of querying posts based on visits and date for current day?
- Extending WP_Query — Optimise SQL query
- How to filter sql only for a specific post type
- How to order category.php loop by ‘meta_value’?
- Order posts by tags count?
- Add and in the header while looping over custom query in page template
- Which custom query am I in and how can I access its properties & methods?
- How to speed up wp_query, took more 5s to run against 100k posts
- How get posts from a subcategory of a category by name?
- How do you query wordpress posts using a math formula between multiple meta field values?
- Custom Post order for homepage
- Is it possible to dynamically get queried term AND taxonomy?
- WP_Query arguments to fetch custom post type posts which are in certain category?
- Pull post meta with post_query?
- How do I reset this wp_list_categories query?
- How to show subcategories using loop?
- $wpdb select date range of posts
- Minimize database queries to user tables?
- Complex Category selection as per user input
- Five posts from a category in footer
- Add quicklink to in the Admin posts page where I can query by a meta_key
- How to get all unique categories for posts in loop?
- Query that loads a custom type AND posts whose ids are not inside those custom types custom fields
- Remove category from query (show all posts in archive.php) pre_get_posts()
- Display Featured Post by Categories and Avoid duplicated posts
- How to use the query hook/filter?
- Order by empty custom field
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- How to pass orderby params to $wpdb->prepare()?
- Modify Search Query if original Query gave no results
- Combine results of multiple WP_Query to resemble single WP_Query
- WordPress Custom Search Form Displaying Unexpected Results
- Yoast primary category query modification
- Slow Query On Search
- Write WP Query that selects posts that are part of the same two categories
- Include one page/post into query which is already returning posts
- Why doesn’t my WP Meta Query return any results?
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- How do I stop the same post showing multiple times in a archive?
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- Related categories order posts by category
- How to get meta key list efficiently?
- Why pagination is not working with tax_query param?
- Display All Top Child Categories / Taxonomy
- I need query_posts() to order results first by a meta value and then by post ID
- WP_Query & Duplicate entries
- WP Query – Search in title or author_name
- URL to Post Archive of Custom Query
- Show posts from categories instead of tags
- Query by meta value (add a dropdown of all values)
- Calculating efficiently on large amount of data generated by wp_query
- Random posts in WP_Query when searching by tag
- Include post_status check within $wpdb query
- Query posts in current category but not attachment format
- query.php – multiple is_category functions
- Create multiple sections for all categories and then queries all the posts for each of those categories
- wp_query – Modify $query to include duplicate content
- Optimising specific Query with ACF meta objects
- How to get several fields from wp_query?
- Horizontally paginate through sets of blog posts?
- Pagination not working with WP_Query (creates links but no page)
- Query custom post type by date field only working when two posts match the query
- meta_query dates from an array
- Divide WP_Query posts by date & post type
- Query Posts by date range with fixed beginning and end
- query by meta value then date and not empty meta value
- New WordPress WP Query using posts from certain categories
- Hide empty categories from get_categories
- A Depth Like Parameter For “get_posts”
- Search query alteration not working for meta values
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2