title: How to Write a SQL Query to Display list of most used tags in the last 30 days in Wordpress website
body: <p>I am looking for a way to display the most used tags over the last 30 days on my Baseball blog, built on Wordpress. 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.</p>
  <p>Here is what I have:</p>
  <pre><code>&lt;ul id=&quot;footer-tags&quot;&gt;
  &lt;?php
  global $wpdb;
  $term_ids = $wpdb-&gt;get_col(&quot;
     SELECT DISTINCT term_taxonomy_id FROM $wpdb-&gt;term_relationships
        INNER JOIN $wpdb-&gt;posts ON $wpdb-&gt;posts.ID = object_id
        WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) &lt;= $wpdb-&gt;posts.post_date&quot;);
  
  if(count($term_ids) &gt; 0){
  
    $tags = get_tags(array(
      'orderby' =&gt; 'count',
      'order'   =&gt; 'DESC',
      'number'  =&gt; 28,
      'include' =&gt; $term_ids,
    ));
  foreach ( (array) $tags as $tag ) {
  echo '&lt;li&gt;&lt;a href=&quot;' . get_tag_link ($tag-&gt;term_id) . '&quot; rel=&quot;tag&quot;&gt;' . $tag-&gt;name . '&lt;/a&gt;&lt;/li&gt;';
  }
  }
  ?&gt;
  &lt;/ul&gt;
  </code></pre>
  <p>The website is slightly less than ~4 weeks old, so to test, I changed <code>INTERVAL 30 DAY</code> to <code>INTERVAL 3 DAY</code> 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.</p>
  <p>To check that the correct number of days have been queried, I did the following:</p>
  <p>Completely deleted all items in the trash for posts and pages, I don't have any custom post types.</p>
  <p>Did the same with drafts.</p>
  <p>Ran a query in phpmyadmin to delete all post revisions - <code>DELETE FROM wp_posts WHERE post_type = &quot;revision&quot;;</code></p>
  <p>Ran a query in phpmyadmin to check if the results are the posts from the last 3 days - <code>SELECT * from wp_posts WHERE DATE_SUB(CURDATE(), INTERVAL 3 DAY) &lt;= post_date</code></p>
  <p>The results from the phpmyadmin query were, in fact, the posts from the last 3 days, but the front-end display did not change.</p>
  <h1>UPDATE</h1>
  <p>Here are some screen shots. Maybe the screenshots can help find where my code is wrong.</p>
  <p><strong>Blog Post with Category and Tags</strong></p>
  <p><img src="https://i.stack.imgur.com/ZTTAo.png" alt="Image of blog post" /></p>
  <p><strong><code>wp_posts</code> table with the post <code>ID</code> of above post</strong></p>
  <p><img src="https://i.stack.imgur.com/mZOVs.png" alt="wp_posts" /></p>
  <p><strong><code>wp_terms</code> table with the <code>term_id</code> of the tags used</strong></p>
  <p><img src="https://i.stack.imgur.com/F3tpl.png" alt="wp_terms" /></p>
  <p><strong><code>wp_term_taxonomy</code> with the tags' <code>term_id</code> as <code>term_taxonomy_id</code></strong></p>
  <p><img src="https://i.stack.imgur.com/iNpIH.png" alt="wp_term_taxonomy" /></p>
  <p><strong><code>wp_term_relationships</code> with <code>term_taxonomy_id</code> assigned to post as <code>object_id</code></strong></p>
  <p><img src="https://i.stack.imgur.com/PTr44.png" alt="wp_term_relationships" /></p>
  <h1>UPDATE 2</h1>
  <p>I think I figured out the problem, but do not know how to fix it.</p>
  <p>The SQL query gets the <code>term_taxonomy_id</code>, not the actual tag ID and <code>get_tag_link</code> uses <code>term_id</code></p>
  <h1>UPDATE 3</h1>
  <p>I have recently created a plugin to display the most popular recently used tags - <a href="https://wordpress.org/plugins/recent-popular-tags/" rel="nofollow noreferrer">https://wordpress.org/plugins/recent-popular-tags/</a></p>