0

I have a simple foreach loop that outputs an ordered list from a CPT 'post_subscribers' and an associated meta key called 'project'.

$theposts = get_posts( array(
    'post_type'   => 'post_subscriber',
    'post_status' => 'publish',
    'numberposts' => -1,
) ); 


echo '<ol>';
    foreach( $theposts as $p ): 

        $project_name = get_post_meta($p->ID, 'project', true);

        echo '<li class="mb-3">';

            echo get_the_title($project_name);
        
        echo '</li>';
    
    endforeach;

echo '</ol>'; 

echo 'Total projects - '. count($theposts)."</br>";

My list will generate:

  1. Project title one
  2. Project title two
  3. Project title two
  4. Project title two
  5. Project title three
  6. Project title one

Total projects - 6

Q) Within my generated ordered list there are several exact matching '$project_name'(s). How can I count the multiple matching '$project_name' entries and display the count for each of them as separate results? (As I have 300+ in my generated list).

EG result: There are 3 projects with the name 'Project title two'.

It seems like a relatively simple task but I can't get my head around it? Many thanks in advance :-)

1 Answer 1

0

I would get the count of a variable number of repeated titles by using an indexed array.

$theposts = get_posts( array(
    'post_type'   => 'post_subscriber',
    'post_status' => 'publish',
    'numberposts' => -1,
) ); 

// project titles counter, outside the loop
$project_titles = array();

foreach( $theposts as $p ): 

    $project_name = get_post_meta($p->ID, 'project', true);
    echo '<ol>';
        echo '<li class="mb-3">';
            $project_title = get_the_title($project_name);
            echo $project_title;

            // check if $project_titles contains $project_title already
            if ( array_key_exists( $project_title, $project_titles) ) :
                $project_titles[$project_title]++;
            else : // otherwise, initialize key value pair with 1
                $project_titles[$project_title] = 1;
            endif;
        
        echo '</li>';
    echo '</ol>';    
endforeach;

// loop through and print all
foreach ($project_titles as $project_title => $project_title_count) :
    if ($project_title_count > 1) : // repeated
        echo 'There are ' . $project_title_count . ' projects with the name ' . $project_title . '.<br/>';
    endif;
endforeach;

$project_titles will now store the number of times each title is repeated as a key-value pair. The key will be your project's title, and the value will be the number of times that project's title exists.

EDIT: Removed first code block since it wasn't useful with question update. Updated second block of code to accommodate a variable number of repeated titles.

6
  • Hi @YourManDan, that was very helpful :-) and worked well for a '$project_title' conditional check against a single static string (IE: 'Project title two'). I have over 300+ projects with multiple exact matching entries (In my list). In the second (2) solution you have provided - what would be the approach / conditional check for counting multiple matching entries dynamically? (I ask politely). Any help appreciated :-)
    – dekkyd
    Commented Feb 27, 2023 at 16:55
  • @dekkyd If I understand correctly, you're wanting a way to output (as per your example): "There are 3 projects with the name 'Project title two'. There are 2 projects with the name 'Project title one'." etc. You want a way to get the count of all projects with the same title?
    – YourManDan
    Commented Feb 27, 2023 at 17:00
  • Correct! and thanks for getting back to me :-)
    – dekkyd
    Commented Feb 27, 2023 at 17:15
  • I have also been looking at this other post: wordpress.stackexchange.com/questions/296752/… for some guidance?
    – dekkyd
    Commented Feb 27, 2023 at 17:55
  • @dekkyd Ah excellent timing, was literally typing out basically that post as you commented :) slightly different, but I've updated my answer now. Please update your question to clarify the 'multiple matching entries dynamically' clarification you had for me for any future passing-byers.
    – YourManDan
    Commented Feb 27, 2023 at 18:07

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.