• I’m building out a custom archive widget that displays a select dropdown of months for each year.

    How can I extend this query to to pass $categories[0]->term_id?

    This is my query so far…

    $categories = get_the_category();

    $years_query = "SELECT DISTINCT YEAR(post_date)
    FROM $wpdb->posts AS p
    WHERE p.post_status = 'publish'
    AND p.post_type = 'post'
    ORDER BY p.post_date DESC";
    
    $years = $wpdb->get_col($years_query);

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to INNER JOIN the term_relationships table on post/object ID and INNER JOIN term_taxonomy table on term_taxonomy_id. term_id and term_taxonomy_id are usually the same, but that is not a given.

    You will get all term IDs assigned to the post, the first listed should be $categories[0].

Viewing 1 replies (of 1 total)
  • The topic ‘Query distinct years in a category’ is closed to new replies.