magicdustwebsites
8/18/2016 - 10:55 AM

WordPress function for getting the most used terms in a given period

WordPress function for getting the most used terms in a given period

<?php
/**
 * Get the most used terms of a given taxonomy in a given period.
 *
 * @param string $taxonomy Taxonomy name.
 * @param int    $number   Number of top terms to retrieve. Default 10.
 * @param string $since    strtotime() compatible string for date to check posts
 *                         published since. Default '-1 month'.
 *
 * @return array|bool An array of WP_Term objects. False if taxonomy does not exist.
 */
function prefix_get_top_terms( $taxonomy, $number = 10, $since = '-1 month' ) {
    $tax_object = get_taxonomy( $taxonomy );
    
    /* Make sure taxonomy exists. */
    if ( ! $tax_object ) {
        return false;
    }
    
    /* Initialize variables. */
    $terms = array();
    $term_names = array();
    $term_objects = array();
    
    /* Get all posts from last 30 days of types for which taxonomy is used. */
    $posts = get_posts( array(
        'post_type'      => $tax_object->object_type,
        'posts_per_page' => -1,
        'no_found_rows'  => true,
        'date_query'     => array(
            'after' => $since,
        ),
    ) );

    /**
     * For each post, get terms used. 
     * In an array add the slug of the term for each use so the most 
     * popular terms can be determined.
     * 
     * In another array, store the WP_Term objects in an array using the slug as
     * a key so we don't need to query them again later.
     * 
     */
    foreach ( $posts as $post ) {
        $post_terms = get_the_terms( $post, $taxonomy );

        if ( $post_terms ) {
            foreach ( $post_terms as $post_term ) {
                $term_names[] = $post_term->name;
                $term_objects[$post_term->name] = $post_term;
            }
        }
    }

    /* Count uses of each term. */
    $term_counts = array_count_values( $term_names );

    /* Sort them from highest to lowest. */
    arsort( $term_counts, SORT_NUMERIC );

    /* Flip array so we have an array of term names, sorted by most popular. */
    $term_names = array_keys( $term_counts );

    /* Get just the number of terms needed. */
    $term_names = array_slice( $term_names, 0, $number );

    /* Using the term name get the WP_Term objects from the objects array. */
    foreach( $term_names as $term_name ) {
        $terms[] = $term_objects[$term_name];
    }
    
    return $terms;
}
<?php
// List of links for up to 10 of the most used categories in the last month.
$top_terms = prefix_get_top_terms( 'category' );
echo '<ul>';
foreach ( $top_terms as $top_term ) {
    echo '<li><a href="' . get_term_link( $top_term ) . '">' . $top_term->name . '</a>';
}
echo '</ul>';

// List of links for up to 5 of the most used tags in the last 15 days.
$top_terms = prefix_get_top_terms( 'tag', 5, '-15 days' );
echo '<ul>';
foreach ( $top_terms as $top_term ) {
    echo '<li><a href="' . get_term_link( $top_term ) . '">' . $top_term->name . '</a>';
}
echo '</ul>';