-1

I have a custom taxonomy called "tasks" shared between two custom post types of "roles" and "products".

Expected results: I need to count how many times a given "task" term is used for each "role" post type minus the count shared by a "product" post type using the same "task" term.

Actual results: None to report as I don't know how to tackle this

Error messages: None

What I've tried: Google keeps coming back with how to count the post type, not the terms :(

The code below is my plan B. Here I am just doing some micro loops on each post-type (only one shown below for brevity), getting the tasks and turning them into an associative array via array_count_values

    $taskList = [];
        
    $queryRoles = new WP_Query( array(
        "post_type" => "roles"
    ));

    while ( $queryRoles->have_posts() ) : $queryRoles->the_post();
        $tasks = get_the_terms( get_the_ID(), "tasks" );
        foreach ( $tasks as $task ) :
            $taskList[] = $task->name;
        endforeach;
    endwhile;

    $overlappingRoleTasks = $taskList;

    print_r(array_count_values($taskList));
Guy Meyer
  • 97
  • 1
  • 7

1 Answers1

0

you could do a loop restricted to only posts with tasks assigned and one restricted to only products with assigned tasks. Then using sizeof(); which return the number of elements in an array, count each number of posts and products and then do some basic php maths.

More about sizeof(); @ https://www.w3schools.com/php/func_array_sizeof.asp

amarinediary
  • 2,583
  • 3
  • 16
  • 25