0

I have the following code:

function auto_link_post_titles( $content, $post_id, $field ) {
    $excluded_ids = array(); // Put the IDs of the pages you want to exclude here
    $excluded_field_names = array(); // Put the names of the fields you want to exclude here
    
    if ( isset( $_GET['ntb'] ) || did_action( 'ninja_tables_loaded' ) ) {
    return $content;
}
    
    if(!in_array($post_id, $excluded_ids) && !in_array($field['name'], $excluded_field_names)) {
        // Sets the post type(s) we'll retrieve.
        $desired_post_types = array( 'my_post_type', 'my_other_post_type' );
        // This is to get all published posts from all post types.
        $args = array(
            'post_type'      => $desired_post_types,
            'post_status'    => 'publish',
            'posts_per_page' => -1, // this is to retrieve all posts
        );

        $posts = get_posts( $args );

        // Here are looping through each post and replace the title in the content with a link.
        foreach ( $posts as $post ) {
            $post_title = esc_html( $post->post_title ); // Here we are getting the post title.
            $post_link  = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.

            // Create a link element
            $link = '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a>';

            // Here we are replacing the plain text title with the link.
            $content = str_replace( $post_title, $link, $content );
        }   
    }

    return $content;
}

// We have apply the function to the content of posts.
// Note the number of parameters is set to 3 here
add_filter( 'acf/load_value', 'auto_link_post_titles', 10, 3 );

My problem is that the code does not take into account the full title. Two different post titles:

  1. POA
  2. POA (+)

For the second title, the code links only the entry matching "POA", and does not take into account that there is an entry that matches, i.e. POA (+).

How could it be resolved so that it takes into account the full title and links to the post with the correct title?

1 Answer 1

0

The issue arises because str_replace() is replacing partial matches (like "POA" inside "POA (+)"). To fix this and ensure only exact matches are replaced, we can switch to using regular expressions with preg_replace().

Here’s a refined version of your function:

function auto_link_post_titles( $content, $post_id, $field ) {
    $excluded_ids = array(); 
    $excluded_field_names = array(); 
    
    if ( isset( $_GET['ntb'] ) || did_action( 'ninja_tables_loaded' ) ) {
        return $content;
    }

    if ( !in_array( $post_id, $excluded_ids ) && !in_array( $field['name'], $excluded_field_names ) ) {
        $desired_post_types = array( 'my_post_type', 'my_other_post_type' );

        $args = array(
            'post_type'      => $desired_post_types,
            'post_status'    => 'publish',
            'posts_per_page' => -1,
        );

        $posts = get_posts( $args );

        foreach ( $posts as $post ) {
            $post_title = esc_html( $post->post_title );
            $post_link  = esc_url( get_permalink( $post->ID ) );

            $link = '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a>';

            // Escape special characters and match exact title
            $escaped_post_title = preg_quote($post_title, '/');
            $content = preg_replace('/\b' . $escaped_post_title . '\b/u', $link, $content);
        }   
    }

    return $content;
}
2

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.