0

I'm working both locally and remotely, pushing code via git. I have the current code set up below, which looks for tags and outputs them as a list using wp_list_categories. I need to use the include parameter as there is no hierarchy with tags (I can't use categories in this instance).

However, as I am working locally and remotely, there's a chance that the IDs from local to remote won't be the same. So, ideally, I'd like to include by name or slug, but I know the include parameter only allows IDs.

Is there any way to include either name or slug in the include parameter?

Idea: My friend said we could have a list of names, and loop through them all and use get_term_by to get the id – but I'm not exactly sure where to start...

Thanks in advance, R

<?php $sector_args = array(
    'taxonomy' => 'post_tag',
    'show_count' => 0,
    'pad_counts' => 0,
    'title_li' => '',
    'include' => '5,6,7'
); ?>

<ul>
    <?php wp_list_categories( $sector_args ); ?>
</ul>

1 Answer 1

1

I managed to figure this out with help from a friend. In case this helps anyone else, here's my code.

<?php $sector_terms = array('animal-health', 'sustainability-science', 'technical-publishing');
$sector_ids = array_map( function( $sector_term )
{ $st = get_term_by('slug', $sector_term , 'post_tag'); return $st->term_id; }, $sector_terms); ?>

<?php $sector_args = array(
    'taxonomy' => 'post_tag',
    'show_count' => 0,
    'pad_counts' => 0,
    'title_li' => '',
    'include' => $sector_ids
); ?>

<ul>
    <?php wp_list_categories( $sector_args ); ?>
</ul>
2
  • There is a query every time get_term_by runs. This is a functional answer but if your list is long you could see performance problems.
    – s_ha_dum
    Commented Dec 2, 2013 at 16:37
  • @s_ha_dum Thanks for this. Any help with an alternative solution? Commented Dec 2, 2013 at 22:34

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.