0

Is there a way to get all tags whose slug starts with a specific word? I tried to use the below code but doesn't output anything. What am i missing here? I am new to php so excuse if i am doing something totally wrong.

    $auth = get_term_by('slug', 'author-', 'tag');
print "values".$auth;
2
  • The part where you've written "author-" is the place for the name so it might literally be looking for "author-" versus "author-shakespeare". Also, the example on the codex has "tag" written as "post_tag" - so you can try changing that. To test whether your function is working, you can also try putting in a whole tag that you're confident works, so if you had the tag "blue" and you insert "blue" in where "author-" is, then you know your logic is right, it's just not finding what it's looking for, usually because the term doesn't exist. codex.wordpress.org/Function_Reference/get_term_by
    – RachieVee
    May 1, 2014 at 15:37
  • @RachieVee Thanks for guidance, as you said i tried to change the code into something like this: $auth = get_term_by('slug', 'cars', 'post_tag'); but still nothing gets printed out. Any idea ? Also i have my tag base renamed to vehicles.
    – newbie
    May 1, 2014 at 15:57

2 Answers 2

0

I think its easier to get them all and filter the ones starting with author:

<?php
$tags = get_terms( 'tags', array(
    'hide_empty' => 0
));
if ( !empty( $tags ) && !is_wp_error( $tags ) ): ?>
    <ul>
    <?php foreach ( $tags as $tag ): ?>
        <?php if(strpos($tag->slug,'author-') !== false): ?>
        <li><?php echo $tag->name; ?></li>
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

If you have lots of tags, you can use the transient api to store the results. I didn't tried this, but it should work fine. This will store the tags for 1 day:

http://pastebin.com/dNVkuHAb

  • Also, you might want to store only the necessary info in the transient api, not the whole array of stuff, so only the name and the id would be fine, but you get the idea.
3
  • Thanks a lot for this, just had to change the get_terms parameter from tags to post_tag. Worked perfect. One question, if i have more than 1000 tags will this code be still performance efficient?
    – newbie
    May 1, 2014 at 17:32
  • I guess the easy answer is, there's no way to get all tags with the desired string without checking all tags. One way or another you have to sift through them all. Someone may have a more clever way to reduce processing time, but I don't know what it would be. P.S. I hate it when someone posts the same answer while I'm writing.
    – unifiedac
    May 1, 2014 at 18:06
  • Check the updated answer for a solution that works better when you have lots of tags
    – passatgt
    May 2, 2014 at 20:49
1

You could get all the terms then cycle through each to check for the desired value:

$my_term = 'author-';
$terms = get_terms( 'post_tag', 'hide_empty=1' );
     if ( !empty( $terms ) && !is_wp_error( $terms ) ){
         echo "<ul>";
         foreach ( $terms as $term ) {
           if (strpos($term->name, $my_term) !== false) 
           {       
          echo '<li>' . '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
           } 
         }
         echo "</ul>";
     }

This code will create an unordered list with a link to each tag.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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