Problem: I am using the Connect Existing dialog to connect existing posts in the post relationship panel of the post editing screen. I have several posts with similar names, so I would like to filter the post titles somehow to include additional identifying information.
Solution: Use a custom code snippet hooked into posts_results to filter these titles:
// Toolset Support Reference: https://toolset.com/forums/topic/filter-to-modify-the-options-within-the-connect-existing-dialog/
// adjust post titles in Connect Existing dialog of relationship editor panel
function ts_filter_connect_existing_titles( $posts, $query ){
$post_types = ['band', 'event']; // slugs of post types
$rel_slugs = ['band-event']; // slugs of post relationships
//We only want to run this if User is searching for related content for specific relationships using the Connect Existing dialog
if ( defined('DOING_AJAX')
&& isset($_REQUEST['action'])
&& $_REQUEST['action']=='types_related_content_action'
&& isset($_REQUEST['relationship_slug'])
&& in_array($_REQUEST['relationship_slug'], $rel_slugs)
&& isset($query->query['post_type'][0])
&& in_array($query->query['post_type'][0], $post_types)
) {
foreach($posts as $post){
// modify the post title as desired here
$post->post_title = 'Test-' . $post->post_title;
}
}
return $posts;
}
add_action( 'posts_results', 'ts_filter_connect_existing_titles', 99, 2 );
You can adjust the post type slugs band and event, as well as the post relationship slug band-event to match your content. This code will manipulate the titles of posts in any of the post types included in the $post_types array, in the Connect Existing dialog for any of the post relationships included in the $rel_slugs array. Modify the post titles however you'd like in the foreach loop. Note that this does not extend search capabilities.