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

post-title block uses get_the_permalink() function with post_id as a parameter #46417

Open
hbhalodia opened this issue Dec 9, 2022 · 0 comments
Labels
[Block] Post Title Affects the Post Title Block [Type] Enhancement A suggestion for improvement.

Comments

@hbhalodia
Copy link
Contributor

Description

Issue Description

For example, if we are working to create the fake WordPress posts to use it on particular instance without saving it to database. We have to do because we need posts array in the form of WP_Post instance.

Since this post is not in database, then calling the function with passing the ID would result out the post to be null, if post is null then its permalink would be return as false.

So in that case the post_link (or other filter) would not work out as it would return early before the filter is being executed, in turn results out to be an empty permalink.

Now suppose, If we have use get_the_permalink() function directly without using the $post_id then, WordPress would try to get the current post instance in the_loop and since it's the WP_Post Instance is not null it would go and try to fetch the permalink based on the current post_name, and we can have a filter post_link to filter out and add our custom link based on the logic.

Here is a link to WordPress core file - https://github.com/WordPress/WordPress/blob/131c378f6a7f2851f8c84295e72bf911ef307747/wp-includes/blocks/post-title.php#L38

The Issue is only getting for WP default theme twentytwentytwo and twentytwentythree, because these themes are using block based approach and to render posts with title we are using post-title block. Also, before twentytwentytwo theme, the themes are using the approach of the_loop and having direct call as the_permalink() or get_permalink() without any ID, so it results out correct.

This use case have been identified while using the search-with-google plugin - Have discovered the codebase for it and seems to have the above issue, hence links on the search results page are not correct. On removing the $post_id from the parameter, everything working as expected for all themes.

If this is a case, then again in the same block post-title we are fetching the title using get_the_title() and not passing any $post_id, which seems to get the above point valid. Here is the link - https://github.com/WordPress/WordPress/blob/131c378f6a7f2851f8c84295e72bf911ef307747/wp-includes/blocks/post-title.php#L23

Possible Solution:

Need to remove the post_id as a parameter from get_the_permalink() and use the current WP_Post instance to fetch the required value.

Step-by-step reproduction instructions

  1. Download, Install and Activate the search-with-google plugin.
  2. Set the required settings as directed by plugin.
  3. Activate any block based theme (probably twentytwentytwo or twentytwentythree thme) which uses post-title block inside query-loop block.
  4. Visit site and add search query parameter to URL and hit enter.
  5. You would see results of search query as shown by the plugin instead of default search.
  6. Hover on the title of posts, you would get no Href and on click would redirect to same page, instead that should go to intended URL of that post which is finded by search engine.
  7. If we remove the $post_id parameter from the post-title block, This would be working as expected.

Screenshots, screen recording, code snippet

Video Link

Environment info

  • WordPress Version - 6.1.1
  • Gutenberg Version - Not Installed ( Using core Gutenberg )
  • Active theme - TwentyTwentyThree
  • Browser - All
  • Device - Macbook Air M1

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Title Affects the Post Title Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

2 participants