Archived topic
External Links break search results
5 replies · Started by troyw on February 3, 2022
Hey team,
I am having a bit of an issue with the search results page. When I enter a search it correctly presents the results. However it also presents results found in the Mega Menu, which are external links. When you click these links in the search results, they do not go to the external link, they take us to a blank page.
Ideally I would prefer the search does not show these links at all, but if it does, it needs to grab the actual link from the menu, not give a faulty link.
Any ideas how I can fix this?
Thanks
Hi Troyw,
By default, WordPress search shouldn’t query for Custom Links.
See this for reference: https://share.getcloudapp.com/6quEJPR4
To have a better understanding of the issue, may I know how you added these external links and playlists to your website?
Kindly see: https://share.getcloudapp.com/9ZuDyBY5
Hope to hear from you soon!
Hey Fernando,
The Menu receives the Playlists by code in the functions.php file, which grabs them from Custom Posts. Here is the code;
//Add playlists to menu
foreach($playlists as $playlist) {
$link = array(
'title' => $playlist->post_title,
'ID' => $playlist->ID,
'url' => get_post_meta($playlist->ID, 'spotify_widget_id', true),
'menu_item_parent' => $playlists_id,
'db_id' => $playlist->ID,
'classes' => array(),
'target' => '_blank',
'current' => ''
);
array_unshift($items, (object) $link);
}
I'm not sure why the search doesn't find this correct link
Hi Troyw,
If that’s the case, to remove a Custom Post Type from the search functionality, can you try adding this PHP snippet:
add_action( 'init', 'update_my_custom_type', 99 );
function update_my_custom_type() {
global $wp_post_types;
if ( post_type_exists( 'mycptslug' ) ) {
$wp_post_types['mycptslug']->exclude_from_search = true;
}
}
Kindly replace mycptslug with the slug of your Custom Post Type.
Please let me know if this works! Hope to hear from you soon! :)
Fernando you are awesome. Thanks so much for that, worked like a dream!
You're welcome! Glad that worked! :)