• I’m trying to use get_terms in order to display the top 10 terms in my custom taxonomy in the sidebar. However, I’m just wondering if there’s any parameter I can use in order to limit the top 10 terms used in posts in a period, like for example in the last 30 days?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Not for get_terms(), because terms are not associated with dates. Terms are associated with posts, which are associated with dates.

    So you would need to get all posts in a certain period, then loop through the posts and compile a list of terms and associated post counts. Once all the posts have been processed, you can sort the term list by count to determine the top 10 terms for the date period.

    Not as simple as adding a parameter, but you’ll find the resulting code to do this isn’t that extensive.

    I’m not sure which terms make the cut if, say numbers 9, 10, 11, 12 in the list all have the same count. PHP will make a determination regardless, but it may not be what you want. Or maybe it doesn’t matter and whatever PHP decides is fine.

    Thread Starter deuts

    (@deuts)

    Yes it doesn’t matter what PHP decides actually.

    Thanks for the response.

    Thread Starter deuts

    (@deuts)

    With the shiny new WordPress 4.6 and the new WP_Term_Query class, I wonder if this is now possible?

    Moderator bcworkz

    (@bcworkz)

    Sorry, no. The new class extends the ways you can query for terms, but does not create new term properties. Dates are still not associated with terms.

    However, in re-reading your original post, maybe I took your question the wrong way, or at least I have an idea for another approach. It’s simple to query for all posts published in the last 30 days. You should be able to process the posts returned in some way to get the 10 most common terms.

    It’s not a simple query proposition. You’d need to loop through all posts returned and keep a running tally of terms used. That tally list can then be sorted by number of times used in posts to get what I believe you are looking for.

    Edited to add: I just saw your other thread with Aubrey, it appears we’re thinking along the same lines with getting a list of terms from all posts within a period.

    Thread Starter deuts

    (@deuts)

    @bcworkz — thank you very much for that help. Unfortunately, my coding skills are not that advanced (yet, perhaps), so this would apparently be out of my grasp. Thank you once again.

    Hell, I’m always up for a challenge. I created this function:
    https://gist.github.com/JakePT/9d6c3dee233f2cd8b1345ed3fad83a69

    It takes a taxonomy, number of terms you want, and the period you want to check (a strtotime() string for a date after which to check posts) and returns an array of WP_Term objects for the top X (10 by default) terms since the time given (1 month by default).

    So you’d add that code to your theme/plugin and use it like:

    $top_terms = prefix_get_top_terms( 'my_custom_taxonomy' );
    
    echo '<ul>';
    
    foreach ( $top_terms as $top_term ) {
        echo '<li><a href="' . get_term_link( $top_term ) . '">' . $top_term->name . '</a>';
    }
    
    echo '</ul>';

    I coded it fairly blind, so no guarantees, but it should do what you’re after.

    PS: I’d recommend changing prefix_ to your own prefix.
    PPS: I’m assuming by ‘top 10 terms’ you mean most used terms.

    Thread Starter deuts

    (@deuts)

    @jacob Peattie –> you are heaven-sent.

    First of all, your script didn’t work as is. I had to debug some in order to make it work. But the codes in the lines 26 to 33 was particularly useful. I wasn’t aware such parameters exist!

    This is how I finally was able to make it work:

    • In line 13, I changed the $taxonomy variable declaration to $tax_object. Same thing I did in line 27
    • In lines 49 and 50, I changed the $post_term->term_name to $post_term->name.
    • In line 59, I changed rsort function to arsort to maintain $key->$value association
    • In line 62, I changed array_flip function to array_keys. The problem with array_flip is if there are multiple values in the array, only the last key will be returned.
    • In line 65, I changed $terms to $term_names. You might have just missed this.

    That’s it, and now it works!

    Thank you once again!

    Thanks for the feedback @deuts. Not bad for coding blind if I do say so myself! ??

    I’ve incorporated your fixes into the gist, in case someone else stumbles across it and finds it useful.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Limit get_terms within a period’ is closed to new replies.