WordPress.org

Ready to get started?Download WordPress

Forums

Display list of most used tags in the last 30 days (1 post)

  1. Travis Pflanz
    Member
    Posted 2 years ago #

    I am looking for a way to display the most used tags over the last 30 days. I am no coder, but I have come up with this mashup to display a list of the most used 28 tags (preference to fit my theme). I cannot, for the life of me, figure out how to limit the tags to the most used in the last 30 days.

    This is what I started off with:

    <ul id="footer-tags">
        <?php
            $tags = get_tags( array('orderby' => 'count', 'order' => 'DESC', 'number'=>28) );
                foreach ( (array) $tags as $tag ) {
                echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a></li>';
                }
        ?>
    </ul>

    Through a little more research, I have come up with this:

    <ul id="footer-tags">
    <?php
    global $wpdb;
    $term_ids = $wpdb->get_col("
       SELECT DISTINCT term_taxonomy_id FROM $wpdb->term_relationships
          INNER JOIN $wpdb->posts ON $wpdb->posts.ID = object_id
          WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= $wpdb->posts.post_date");
    
    if(count($term_ids) > 0){
    
      $tags = get_tags(array(
        'orderby' => 'count',
        'order'   => 'DESC',
        'number'  => 28,
        'include' => $term_ids,
      ));
    foreach ( (array) $tags as $tag ) {
    echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a></li>';
    }
    }
    ?>
    </ul>

    The website is slightly less than ~4 weeks old, so to test, I changed INTERVAL 30 DAY to INTERVAL 3 DAY and the tags being returned seem random and some haven't been used in 2+ weeks and have only been used a single time. As well, only 8 tags are being displayed, when more have been used.

    To check that the correct number of days have been queried, I did the following:

    Completely deleted all items in the trash for posts and pages, I don't have any custom post types.

    Did the same with drafts.

    Ran a query in phpmyadmin to delete all post revisions - DELETE FROM wp_posts WHERE post_type = "revision";

    Ran a query in phpmyadmin to check if the results are the posts from the last 3 days - SELECT * from wp_posts WHERE DATE_SUB(CURDATE(), INTERVAL 3 DAY) <= post_date

    The results from the phpmyadmin query were, in fact, the posts from the last 3 days, but the front-end display did not change.

Topic Closed

This topic has been closed to new replies.

About this Topic