I'm using a plugin (IssueM) which gives the "articles" post-type a custom taxonomy called "Issues".

I need to find and store in a variable the most recently created one of this taxonomy. So, for example, within Issues I have issue-one through to issue-five - I need to return issue-five as it was created most recently.

How can I access this custom taxonomy by date?

  • WordPress doesn't store creation or modification dates for taxonomy terms, so @dougvdotcom's answer below (sorting by the auto-incremented ID#) is going to be your best bet. – Travis Seitler Mar 16 '15 at 15:33

Use get_terms to select that custom taxonomy, ordering by id DESC and returning only one record:

$latest_term = get_terms( 'issuem_issues', array( 'orderby' => 'id', 'order' => 'DESC', 'number' => 1, 'hide_empty' => false );

This presumes that your WP DB is set up normally, so the highest ID number on the custom taxonomy terms will be the most recently added term.

Also, make sure the slug for the custom taxonomy is Issues, not issues. The slug query is case-sensitive.

| improve this answer | |
  • Thanks for this. Problem is when I do this, I get "invalid taxonomy" returned. I have tried both "Issue" and "Issues" just in case. – Francesca Mar 16 '15 at 15:36
  • The correct taxonomy, I believe, will be issuem_issue if my examination of the plugin's code is correct. – user12479 Mar 16 '15 at 15:38
  • You are correct. Changed to issuem_issue and gets the most recent. Although it will only get the most recent that has posts applied to it. So even though issue-five exists, it returns issue-four as issue-five has no live articles. – Francesca Mar 16 '15 at 15:43
  • You can fix this by setting 'hide_empty' to false. Code corrected above. – user12479 Mar 16 '15 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.