0

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?

1
  • I would recommend asking this on the main Stack Exchange site as it appears to be PHP question not-specific to WordPress.
    – jdm2112
    Oct 19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.