Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created January 20, 2026 11:49
Show Gist options
  • Select an option

  • Save searchwpgists/df1f4721da516ef76f9386d778945f43 to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/df1f4721da516ef76f9386d778945f43 to your computer and use it in GitHub Desktop.
Update media attachment results to link to their parent post
<?php
add_filter( 'searchwp_live_search_results_entry_data', function( $data, $result ) {
if ( $result instanceof \WP_Post && $result->post_type === 'attachment' ) {
$parent_id = wp_get_post_parent_id( $result->ID );
if ( $parent_id ) {
$data['permalink'] = get_permalink( $parent_id );
}
}
return $data;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment