Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Canonical URL of posts in search #287

Open
yogeshbeniwal opened this issue Jan 17, 2023 · 8 comments
Open

Show Canonical URL of posts in search #287

yogeshbeniwal opened this issue Jan 17, 2023 · 8 comments

Comments

@yogeshbeniwal
Copy link

Describe the bug
When posts are assigned multiple categories and permalink is set as /%category%/%postname%/, same post is accessible from multiple url, e.g. [Domain]/Category1/post-slug and [Domain]/Category2/Child2/post-slug. When Yoast SEO plugin is used and setting up Child2 as primary category, a canonical URL is set for all other URLs pointing to URL with primary category f.g. [Domain]/Category2/Child2/post-slug.

Currently Algolio search shows URL in search as [Domain]/Category1/post-slug instead of [Domain]/Category2/Child2/post-slug.

To Reproduce
Steps to reproduce the behavior:

  1. Set Permalink structure to /%category%/%postname%/
  2. Install Yoast SEO plugin
  3. Create Category1 category
  4. Create Category2 --> Child2 Category
  5. Publish a post with all categories and primary category as child2
  6. Observe that search for post is taking user to [Domain]/Category1/post-slug instead of [Domain]/Category2/Child2/post-slug

Expected behavior
Search shall take canonical URLs in account and direct user to [Domain]/Category2/Child2/post-slug instead of [Domain]/Category1/post-slug

@tw2113
Copy link
Member

tw2113 commented Jan 17, 2023

Hi @yogeshbeniwal

This is very likely going to come down to what information is being indexed as well as what indexed content on a given object is being used for the generated link in either the Autocomplete or the InstantSearch experiences.

We'd need to get the correct permalinks added to a given post's index as well as customize the template in question. If you want/need, I can point you to the correct documentation for both of those parts, and help answer some questions about that as needed.

The wiki is a pretty good starting area https://github.com/WebDevStudios/wp-search-with-algolia/wiki for example the custom attributes section https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Custom-Attributes

@yogeshbeniwal
Copy link
Author

@tw2113 Thank you. Can you share a code snippets for use to resolve this?

@tw2113
Copy link
Member

tw2113 commented Jan 18, 2023

Are you needing this for the Autocomplete integration or the Instantsearch?

The first code snippet at this page would be used for Autocomplete with the algolia_post_shared_attributes filter
https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Custom-Attributes#push-your-visits-count-to-algolia

You'd want to change it to algolia_searchable_post_shared_attributes for InstantSearch.
You can name the attributes whatever you want for example $shared_attributes['my_awesome_attribute'] = 'some attribute'. It's here that you'd need to do some coding work to fetch the intended canonical URL to store in your named and returned attribute. Once you do a re-index of either one or all of your posts, you should start seeing my_awesome_attribute as part of your indexed objects in your Algolia Dashboard.

At that point, you'll need to customize the template file for the appropriate usage, eg Autocomplete or Instantsearch, and have it reference that new property for the link, and using the canonical url stored in that property. For example...

https://github.com/WebDevStudios/wp-search-with-algolia/blob/main/templates/autocomplete.php#L19-L33

On line 22 at the link above, we have the template for Autocomplete and the <a> link using an href of data.permalink. Rolling with my example above, if I stored the canonical link as my_awesome_attribute, I'd need to change that href to data.my_awesome_attribute instead.

You can read about template customization at https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Customize-Templates

@tw2113
Copy link
Member

tw2113 commented Mar 10, 2023

Did you ever make any headway here @yogeshbeniwal

@yogeshbeniwal
Copy link
Author

@tw2113 No haven't. I was hoping this functionality will be available out of box.

@tw2113
Copy link
Member

tw2113 commented Mar 11, 2023

Admittedly, we aim to keep the WP Search with Algolia as agnostic as possible, but that also means that we're taking a less out-of-box approach. This allows for others to add in details for their specific site needs, rather than us trying to make assumptions and result in making site owners have to somehow undo those decisions, because they don't apply to their use cases.

I can also say that we're working on a Pro addon for WP Search with Algolia, but that won't be a free one, and some of the features intended for that Pro addon are SEO based. This is something that I could get added to feature ideas if you'd like that, or if you'd prefer some potential help with overriding on your own.

@tw2113
Copy link
Member

tw2113 commented Aug 2, 2023

So I'm definitely seeing the behavior described, but I'm also not seeing any great way, so far, to actually get the generated canonical URL, outside of the value manually saved in the Advanced panel from Yoast SEO. For that, I believe WPSEO_Meta::get_value( 'canonical', $post->ID ) is enough, but not yet on automated versions.

@tw2113
Copy link
Member

tw2113 commented Aug 2, 2023

Doing some more testing with this this morning. I'm DECENTLY certain that when not using the meta field in the Advanced panel, Algolia is receiving the canonical URL. Based on some research and some asking around, I believe Yoast makes use of get_permalink() to output what they determine to be the canonical URL, and that's what we're using already for our index object construction.

That said, the Advanced panel needs some extra finesse. Something like the following looks like it's working from my testing.

ORIGINAL:

function wpswa_support_canonical_post_url( $shared_attributes, $post ) {
	$canonical_meta = WPSEO_Meta::get_value( 'canonical', $post->ID );
	if ( ! empty( $canonical_meta ) ) {
		$shared_attributes['permalink'] = $canonical_meta;
	}
	return $shared_attributes;
}
add_filter( 'algolia_searchable_post_shared_attributes', 'wpswa_support_canonical_post_url', 10, 2 );

New August 3rd 2023:

function wpswa_support_canonical_post_url( $shared_attributes, $post ) {

	// Fetch canonical URL from Yoast, for given post.
	$shared_attributes['permalink'] = YoastSEO()->meta->for_post( $post->ID )->canonical;
	
	return $shared_attributes;
}
add_filter( 'algolia_searchable_post_shared_attributes', 'wpswa_support_canonical_post_url', 10, 2 );

I think this would cover both potential sources, between the meta and the usage of get_permalink()

EDIT: Yoast SEO folks pointed me to YoastSEO()->meta->for_post( $post->ID )->canonical. I was originally assuming just get_permalink() would cover things, along with the WPSEO_Meta::get_value() would work.

EDIT 2: After testing, I have updated the snippet above to make use of the Yoast-provided version.

Bonus: If you want to get a term canonical URL, you can use YoastSEO()->meta->for_term( $term->term_id )->canonical

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants