Counting a WP_Post Object value in an arary, using a filter?

I am trying to check whether a post in the array is the first post with a certain array value. I have a code similar to the one below:

Plugin code

    ?php
    
    function my_function() {
    $entries = $posts-posts;
        foreach( $entries as $index = $entry )
            {   
                $title  = get_the_title( $entry-ID );
                $event = $title;
    
                $output = apply_filters( 'my_filter', $event, $entries, $index );
            }
    
            return $output;
    }
?

Code in my child theme functions

?php 
    function add_stuff( $event, $entries, $index ) {
        //Targeting stuff in the array
        $get_id = $entries[$index]-ID;
    }
    
    add_filter( 'my_function', 'add_stuff', 10, 3 );
    ?

The array $entries looks like this. A certain array can be targetted by $entries[$index]

Array
(
    [0] = WP_Post Object
        (
            [ID] = 403
            [post_title] = My Post Title
        )
    [1] = WP_Post Object
        (
            [ID] = 404
            [post_title] = My Post Title
        )
    [2] = WP_Post Object
        (
            [ID] = 405
            [post_title] = My Post Title 2
        )

)

Now I want to check in my add_stuff filter whether the post that is being processed by the foreach function, is the first one with the given title. As you can see, the first two posts have the same title. I would like to echo something only when it is the first unique title in the array that is being processed. I tried things like count and array_count_values but could not make it work. Any ideas?

Topic array count filters Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.