0

I have a client who wants to review and revise their Tags and tagging strategy, but they want to start with a spreadsheet of ALL tags, including those that have no posts assigned to them.

SO I created a template that outputs the Tags to an HTML Table, that I can then import into Excel and upload to Google Sheets so their team can collaborate on it.

The template works great, but the query does not include the empty Tags, even though I have "hide_empty" set to 0 (tried both 'false' and '0' with the same results).

I can't use get_terms because that query includes terms added by plugins that are not at all related to Posts nor post_tags, even though when I tried get_terms I added the 'taxonomy' => post_tag, it still gave me about 400 additional terms that are not Post Tags (they are mostly for CPTs and the like, not Posts).

If I use get_tags, I get a clean list of just Post Tags, but it isn't giving me the ones with 0 Posts assigned.

Can someone please help me troubleshoot this code?

Here is the HTML and query from my Template:

    <table id="download">
    <tr>
      <th>Tag Name</th>
      <th>Post Count</th>
    </tr>
    <?php
    global $wp_query;
    $tags = get_tags( $args );
    $args = array(
      'hide_empty' => 0,
    );
    if ( $tags ) :
      foreach ( $tags as $tag ) : ?>
    <tr>
        <td><?php echo esc_html( $tag->name ); ?></td>
        <td><?php echo $tag->count; ?></td>
    </tr>
    <?php endforeach; endif; ?>
    </table>

I know there are quite a lot of Tags in their list that have 0 Posts assigned, I can see them in the WP back end, just can't figure out how to export the entire list to something someone can work with in Excel/Google Sheets....

6
  • Development with debug mode off is the complete waste of time. If you turn debug mode on, you will be warned that $args of get_tags() is undefined. Debugging in WordPress
    – Max Yudin
    Nov 11 '21 at 8:32
  • I do have debugging enabled, I did not get any errors related to this code. I would not have posted the question without debugging on my own. Also as you can see, it's not undefined, I DO indeed have defined $args.
    – Trisha
    Nov 11 '21 at 16:47
  • 1
    You have to assign $args = array() before calling get_tags( $args ), like in @shanebp answer. No warnings mean wrong error reporting configuration. See Display All PHP Errors: Basic & Advanced Usage
    – Max Yudin
    Nov 11 '21 at 18:04
  • Actually Max that isn't correct.....you can call your variables ($args) second, there are numerous examples all over the Codex. I don't have the wrong error reporting configuration, it does report errors (when I have them) just fine, it just didn't find any error with my code. The code works fine, it just was ignoring the 'hide_empty' parameter until I specified "post_tags" which shouldn't have been needed since that is the default.
    – Trisha
    Nov 11 '21 at 21:52
  • Your hide_empty was not ignored. It was not set! Btw, Undefined variable: args is not an Error but Notice. I suspect you see errors and don't see notices. Start your file with ini_set('error_reporting', E_ALL); and feel the difference. And, please, show me one of numerous examples you've mentioned to prove your rightness.
    – Max Yudin
    Nov 12 '21 at 11:23
0

Try:

$args = array( 'hide_empty' => false );
$tags = get_tags( $args );

More info re possible args.

For csv export, you probably want to use get_the_tags

1
  • Thank you @shanebp but that is exactly what I'm already doing and as mentioned already both "0" and "false" return the same results - only Tags that have Posts assigned, not the empty ones. Also, get_the_tags is Post-specific, it works on a Post not universally to retrieve all Tags. :-(
    – Trisha
    Nov 11 '21 at 0:19
-1

Well this was an odd fix, and one that in theory shouldn't have been necessary, however, what finally worked (to show ALL Tags including those with empty counts) was to specify the taxonomy => 'post_tag' EVEN THOUGH that is the 'default' parameter and shouldn't need to have been added to $args.

Sigh.

My final query that works:

global $wp_query;
$tags = get_tags( array(
  'taxonomy' => 'post_tag',
  'hide_empty' => false,
  )
);
if ( $tags ) :
  foreach ( $tags as $tag ) :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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