使用php在wordpress中查找相同的标题帖子,并使用自定义短代码更新帖子内容

I have the following code: What the code below does is, whenever the user publishes a new custom post, a short code is automatically inserted in the editor area of the post. i.e. [custom-trick id=234] where 234 is a an example custom post id

function update_post_content( $ID, $post ) 
{
  if (get_post_meta($ID, “first_time_published”, false)) 
  {
    return;
  }

  $my_post = array(
                    ‘ID’ => $ID,
                    ‘post_content’ => “[custom-trick id=$ID]”,
                   );

   update_post_meta($ID, “first_time_published”, true);
   wp_update_post($my_post);
}

add_action( ‘publish_custom-trick’, ‘update_post_content’, 10, 2 );

What I want to do is modify above code in this way:

START: When the custom post is published, the code searches for all published custom posts with identical “Title”.

MATCH NOT FOUND: If it does not find any match, then the code simply does what the above code is already doing i.e. update the content of the custom post with [custom-trick id=234]

MATCH FOUND: If it finds a match, then the code simply does what the above code is already doing PLUS it also adds another shortcode [custom-trick id=234][custom-trick id=342] where id=234 is the id of the matched post and id=342 is id of the current custom post that just got published.

I am new to php/wordpress and I would really appreciate any help... Thanks