简体   繁体   中英

Wordpress: Counting total number of times tags are used

I successfully rejigged existing code to count the total number of posts within a specific tag, and then print the sum. I cobbled this together based off of an existing snippet and although it works, I realize it is very inefficient.

Ideally it would be great if I could count the number of posts in all tags, and return the total so I wouldn't have to hardcode $term_name .

I know theres a loop there somewhere... This is my first time really dealing with PHP so while this is exciting, it reminds me I have a lot to learn.

<?php
$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name

// Using Term Name
$term_name = 'Cookies';
$term = get_term_by('name', $term_name, $taxonomy);

$term_name1 = 'Juice';
$term1 = get_term_by('name', $term_name1, $taxonomy);

$term_name2 = 'Milk';
$term2 = get_term_by('name', $term_name2, $taxonomy);

// Fetch the count, sum and print.
echo ($term->count + $term1->count + $term2->count);
?>

A little clarification: I want to count the posts under each tag, and then sum them, even if double counting posts. I'm using tags as a sort of inventory of a post. Lets say we're using tags to identify items in a picture, you could use this to say 20,000 different items tagged.

Really, first thing you need to do is build a list of terms that are used, then get the counts. Both of these can be done in a single database query.

$taxonomy = 'post_tag'; //the post type you want to look up
global $wpdb; //get instance of database connection
$sql = $wpdb->prepare("SELECT terms.name,taxonomy.count
        FROM wp_terms AS terms 
        INNER JOIN wp_term_taxonomy AS taxonomy ON (terms.term_id = taxonomy.term_id)
        WHERE taxonomy = %s;",$taxonomy);
$results = $wpdb->get_results($sql);

if ( $results ){
    foreach ( $results as $result ){
        echo('<p>'.$result->name.' has a count of '.$result->count.'</p>');
    }   
}

Please note: you should further refine the answer by using $wpdb->prefix instead of hard coding "wp_"...but if this is just for your own use and not for redistribution you don't need to worry about that.

EDIT:

Based on a comment on the original question, an alternate SQL could be:

$taxonomy = 'post_tag'; //the post type you want to look up
global $wpdb; //get instance of database connection
$sql = $wpdb->prepare("SELECT sum(taxonomy.count)
        FROM wp_terms AS terms 
        INNER JOIN wp_term_taxonomy AS taxonomy ON (terms.term_id = taxonomy.term_id)
        WHERE taxonomy = %s;",$taxonomy);
$result = $wpdb->get_var($sql);

echo("There are a total of $result tag matches");

A note: If you have 2 posts each using 3 different tags, you will get a result of 6 (which is what you wanted in your comment...but not what others might want).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM