0

I'm trying to get a list of custom taxonomy terms by date (or possibly slug). What I need is the NEWEST term and it's permalink in a custom taxonomy. I've been trying the code below but it's returning the same term over and over again. The goal is to generate a link to the archive page for the newest term in this taxonomy, which happens to be a bunch of journal editions; so a link to the "Latest Edition."

// latest edition
$taxonomies = array( 
    'jjm_editions'
);              
$args = array(
    'orderby'    => 'date', 
    'order'      => 'DSC',
    'hide_empty' => true,
    'number'     => '3'
);              
$terms = get_terms($taxonomies, $args);
foreach ( $terms as $term ) {
    $term_link = get_term_link( $term );
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
    echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
}

Even if I change ASC to DESC, I always get the first edition.

Edit: I know "number" should be '1'. I have it at three to see what it would output. It still only outputs one term, Edition 6.

7
  • date isn't a valid value for orderby, taxonomy terms have no date.
    – Milo
    Commented Oct 16, 2014 at 17:44
  • What are the valid orderby for taxonomy terms? Name isn't going to work.
    – Josh M
    Commented Oct 16, 2014 at 17:47
  • there isn't a 100% reliable way to do it, the closest you can get is to do this.
    – Milo
    Commented Oct 16, 2014 at 17:51
  • 1
    do the terms have posts associated with them? hide_empty set to true will only return terms with posts.
    – Milo
    Commented Oct 16, 2014 at 18:02
  • 1
    @Milo That was it! Now orderby_id is doing exactly what I want. Thanks! If you'll put that in as an answer I'll give you some thumbs-up love.
    – Josh M
    Commented Oct 16, 2014 at 18:29

1 Answer 1

3

This is the working code:

// latest edition
$taxonomies = array( 
    'jjm_editions'
);              
$args = array(
    'orderby'    => 'ID', 
    'order'      => 'DESC',
    'hide_empty' => false,
    'number'     => '1'
);              
$terms = get_terms($taxonomies, $args);
foreach ( $terms as $term ) {
    $term_link = get_term_link( $term );
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
    echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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