0

I'm working on a checklist within wordpress – I have a huge list of posts and I want to tag them with checks that have been done for the post.

However, I want to display all the tags not applied on this post.

For example:


Sarah's Document

Processes not checked yet: ISU-123, ISU-124, ISU-125


Andrew's Document

Processes not checked yet: ISU-125


I want to apply a reverse tagging method almost – I tag posts that have the processes applied to them and show a visual for process not yet tagged on each post.

Is there a way of using get_the_tags for showing tags not applied? Or perhaps doing a query for all tags and excluding an array?

2 Answers 2

0

if i have understood your question, you want all tags from a taxonomy not applied to your post?

maybe you can use get_terms('your_custom_taxonomy') to get all of your tags, and parse/diff to your current post tags to get all tags not applied to your post https://developer.wordpress.org/reference/functions/get_terms/

or maybe try to use a filter like list_term_exclusions with your current post https://developer.wordpress.org/reference/hooks/list_terms_exclusions/

0

After going through the tags / term functions, I've managed to write a function to solve this issue and wrap it in a function:

function tags_not_applied( ){
  // Get ID of post
  $post_id = get_the_ID();

  // Get post's current tags
  $tags_array = wp_get_post_terms( $post_id, 'taxonomy', array("fields" => "ids") );
  if (empty($tags_array))
    return;

  // Put all tags into array
  $args = array(
    'hide_empty' => false,
    'exclude' => $tags_array
  );

  // Get all tags except the ones applied
  $terms = get_terms( 'taxonomy', $args );

  if (empty($terms))
    return;

  // Create list of tags not applied
  $html = '<ul class="tags_not_applied">';
  foreach ( $terms as $term ) {
    $term_link = get_term_link( $term->term_id );

    $html .= "<li><a href='{$term_link}' title='{$term->name} term' class='patch patch-{$term->slug}'>";
    $html .= "{$term->name}</a></li>";
  }
  $html .= '</ul>';
  echo $html;
}

Thing to note is that get_terms by default has hide_empty as true, which causes the assumption that nothing is being returned!

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.