Skip Navigation

[Resolved] Filter to modify the options within the Connect Existing dialog

This thread is resolved. Here is a description of the problem and solution.

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.

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/posts_results/

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by David 2 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#2129125

Tell us what you are trying to do?
When editing a post and wanting to connect it to another post for which I have established a many-to-many relationship, I click on the Connect Existing button. In the dialog that appears, the posts I can connect to are presented in a select2. My issue with this is that the post names are often repeated (for example, there might be many named "Lesson 1").

I would like to filter those post titles. Is there a filter I can use? If not, where is the code in the Toolset plugin that handles generating that list within the Connect Existing dialog? Thank you.

#2129211

Hi, I was running a quick test and it seems you can use the posts_results action to manipulate the titles shown in the select2.js interface when using the Connect Existing dialog. Here's an example:

// 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. Make sense?

Modify the post titles however you'd like in the foreach loop.

Note that this does not extend search capabilities. For example, if you add the value of some custom field to the title in the foreach loop, you cannot also search using that custom field value to refine the results. The search term is still just the post title, only the displayed post title results are manipulated. Hope this helps.

#2130435

My issue is resolved now. Thank you!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.