1

I am using WordPress Woocommerce as a review platform rather than a eCommerce site. When a review is made, a pre-set arrangement of tags are able to be used for each individual product post. Currently the tags on the product post show the total count site wide. For example, on page "product post A" tag "fast" shows "fast56" but tag "fast" is only used 2 times on "product post A". So it should only show "fast2". How can I make it so the number shows only the number a tag was used in a particular post?

  • 1
    Can you explain a bit more about your tags? It doesn't really make sense right now. As a tag (the post_tag taxonomy) can only be attached to a post once. Are you perhaps referencing to hash tags? – ngearing Jul 9 '19 at 4:43
  • I am using a plugin for grouping tags and I just spoke to the owner. He explained, "The count has the same link as the rest of the tag. The link always goes to a "tag archive" page where all posts are listed that have that tag. That is the normal behaviour of WordPress tag clouds. In order to change that, you need to change the code of the plugin. (Retrieve the permalink of the post with that particular ID and then replace the tag link with that permalink.)" – user171531 Jul 10 '19 at 5:59
  • prnt.sc/ocxryx If you look at the screenshot, it shows the total tag count for the entire site. I need the number associated with the tag to show only for that specific product on that page. – user171531 Jul 10 '19 at 6:02
0

This isn't possible currently with what you've got. Tags or Taxonomies in WordPress can only bad attached to a post once. And are completely ignorant of the content in a post they are attached to.

However, it shouldn't be too difficult to write a function to count the times a tags name is used in the content of a post.

First, we need to run our counting function when a post is updated or created.

add_action( 'save_post', 'count_terms', 10, 2 );

Then our function to count the tags.

function count_terms( $post_id, $post ) {

    $taxonomy = 'post_tag'; // Set taxonomy to count.
    $terms    = get_the_terms( $post_id, $taxonomy );
    if ( ! $terms ) {
        return;
    }

    $term_counts = [];
    foreach ( $terms as $term ) {
        // Words to search the content for.
        $term_search = [];
        $term_search[] = $term->name;
        $term_search[] = $term->slug;
        $term_search = implode('|', $term_search);

        /* Search content excluding text in html tags. '(?!([^<]+)?>)' */
        preg_match_all( "/(?>\b)(?:$term_search)(?>\b)(?!([^<]+)?>)/mi", $post->post_content, $matches );

        // Count matches & push to array.
        if ( $matches ) {
            $term_counts[ $term->slug ] = count( $matches[0] );
        } else {
            $term_counts[ $term->slug ] = 0;
        }
    }

    // Save count.
    update_post_meta( $post_id, "{$taxonomy}_content_counts", $term_counts, get_post_meta( $post_id, "{$taxonomy}_content_counts", true ) );

}

And now we need some way to display the counts. You could add this function to your theme template.

function display_term_counts( $post_id, $taxonomy = 'post_tag', $echo = false ) {

    if ( ! $post_id ) {
        $post_id = get_the_id();
    }

    $counts = get_post_meta( $post_id, "{$taxonomy}_content_counts", true );
    if ( ! $counts ) {
        return false;
    }

    $output = [];

    foreach ( $counts as $term => $count ) {
        $term = get_term_by( 'slug', $term, $taxonomy );

        $output[] = sprintf(
            '<span class="term-count">%s - %s</span>',
            $term->name,
            $count
        );

    }

    // Could add separator here.
    $output = implode( '', $output );

    $output = "<div class='term-counts'>$output</div>";

    if ( $echo ) {
        echo $output;
    }

    return $output;
}

Or wrap it in a shortcode and use it in the content. [term_counts tax='post_tag']

add_shortcode( 'term_counts', function( $atts ) {
    $tax = $atts['tax'];
    if ( ! $tax ) {
        return false;
    }

    return display_term_counts( get_the_id(), $tax );
} );
| improve this answer | |
  • This looks great ngearing. Im sorry but where exactly do I place all of this? – user171531 Jul 17 '19 at 7:37
  • You can add this to your functions.php file in your theme or child theme. – ngearing Jul 17 '19 at 23:00
  • ngearing, I tried adding what you provided and unfortunately it did not work. I also provided it to my developer and they told me it was not giving the desired outcome. I appreciate the try tho. – user171531 Jul 19 '19 at 20:10

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.