0

I have a code that counts how many posts I have in with certain tags, is it possible to add the functions to count how many posts I have with a certain tag that's in a particularly category called "donotallowpost"?

<?php
$term_slugs = [ 'cat', 'bird', 'chinchilla','dog','ferret','hamster','horse','lizard','mouse','pig','poultry','rabbit','snake','turtle' ];
$terms = get_terms( [
    'taxonomy' => 'post_tag',
    'slug'     => $term_slugs,
] );

$post_count = array_sum( wp_list_pluck( $terms, 'count' ) );
echo  $post_count;

2 Answers 2

1

Yes it is possible to count the posts with certain tags within a particular category. You can achieve this by using a combination of wp functions like WP_Query to retrieve posts and get_terms to retrieve the tags within the specified category.

<?php
$category_slug = 'donotallowpost';
$term_slugs = [ 'cat', 'bird', 'chinchilla', 'dog', 'ferret', 'hamster', 'horse', 'lizard', 'mouse', 'pig', 'poultry', 'rabbit', 'snake', 'turtle' ];

// Get the category ID using the slug
$category = get_category_by_slug($category_slug);
$category_id = $category->term_id;

// Get the tags within the specified category
$tags = get_terms( [
    'taxonomy' => 'post_tag',
    'hide_empty' => false, // Make sure to include tags with no posts
    'object_ids' => get_objects_in_term( $category_id, 'category' ), // Get tags associated with the category
] );

// Initialize post count
$post_count = 0;

// Loop through each tag to count posts
foreach ($tags as $tag) {
    $tag_post_count = new WP_Query( array(
        'tag_id' => $tag->term_id,
        'posts_per_page' => -1, // Retrieve all posts
        'fields' => 'ids', // Only retrieve post IDs for performance
    ) );
    $post_count += $tag_post_count->post_count;
}

echo $post_count;
?>
3
  • I added a post, and now it says that 2 posts are "donotallowpost", but there is only 1.. Mar 4 at 17:47
  • and now it claims it 4, so it count something, but not what it's suppose to count.. Mar 6 at 11:16
  • exactly what are you counting? Mar 11 at 17:37
0

After a few hours of consultation with open AI i got this code;

<?php
/*
* Template Name: Count Posts with Animals
*/

// Include WordPress bootstrap file
require_once(ABSPATH . 'wp-load.php');

// Perform the database query
global $wpdb;
$query = $wpdb->prepare("
    SELECT COUNT(p.ID) AS post_count
    FROM {$wpdb->posts} p
    INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
    INNER JOIN {$wpdb->term_taxonomy} tt_tag ON tr.term_taxonomy_id = tt_tag.term_taxonomy_id
    INNER JOIN {$wpdb->terms} t_tag ON tt_tag.term_id = t_tag.term_id
    INNER JOIN {$wpdb->term_relationships} tr_cat ON p.ID = tr_cat.object_id
    INNER JOIN {$wpdb->term_taxonomy} tt_cat ON tr_cat.term_taxonomy_id = tt_cat.term_taxonomy_id
    INNER JOIN {$wpdb->terms} t_cat ON tt_cat.term_id = t_cat.term_id
    WHERE p.post_type = 'post'
    AND p.post_status = 'publish'
    AND (
        (tt_tag.taxonomy = 'post_tag' AND t_tag.slug IN ('cat', 'bird', 'chinchilla', 'dog', 'ferret', 'hamster', 'horse', 'lizard', 'mouse', 'pig', 'poultry', 'rabbit', 'snake', 'turtle'))
        OR (tt_tag.taxonomy = 'post_tag' AND t_tag.term_id IN (
            SELECT term_id FROM {$wpdb->terms} WHERE term_id IN (
                SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE parent IN (SELECT term_id FROM {$wpdb->terms} WHERE slug IN ('cat', 'bird', 'chinchilla', 'dog', 'ferret', 'hamster', 'horse', 'lizard', 'mouse', 'pig', 'poultry', 'rabbit', 'snake', 'turtle'))
            )
        ))
    )
    AND tt_cat.taxonomy = 'category'
    AND t_cat.slug = 'donotallowpost'
");
$post_count = $wpdb->get_var($query);

// Display the result
echo $post_count;

The other answer by @vijay-pancholi didn't work on my page, and according to Aopen A.I it whas these reasons;

Performance: This code uses WP_Query inside a loop to count posts
for each tag individually. This can be inefficient, especially if
there are a large number of tags or posts, as it results in multiple
database queries.

Query Parameters: The tag_id parameter in WP_Query expects a single
tag ID, but you're passing an array of tag IDs obtained from
get_terms. This might not produce the desired results.

Object ID Retrieval: The get_objects_in_term function is used to get
the posts associated with the specified category, but it retrieves
all objects (posts, custom post types, etc.) associated with the
category, not just posts. This can lead to incorrect counts if other
types of objects are associated with the category.

Now I'm not an expert on php, but the later (A.I) code works.

1
  • I must say, that after a few days chatting with openai, it's pretty stupid, so IF you do so, make sure you know little code, because it makes mistakes.. Mar 20 at 11:25

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.