Make WordPress Core

#57295 closed enhancement (reported-upstream)

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

Reported by: hbhalodia's profile hbhalodia Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Editor Keywords:
Focuses: Cc:

Description

Post Title Block is using get_the_permalink() function with parameter as $post_id

Issue:

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](https://wordpress.org/plugins/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 get the expected results.

Change History (5)

#1 @SergeyBiryukov
17 months ago

  • Component changed from General to Editor

#2 @costdev
17 months ago

  • Keywords dev-feedback added

Hi @hbhalodia, welcome to Trac!

As this issue occurs with the Post Title Block, this issue should be reported in the Gutenberg GitHub repository.

Please review the issues related to the Title Block to see if your issue has already been reported. If not, you can create a new feature request.

If you find/create an issue for this, please drop a link to the issue in this ticket and we'll close this ticket as reported-upstream.

Thanks!

#3 @hbhalodia
17 months ago

Thanks @costdev to point this out. I would look into issues and would try to raise in GH issues if not.

#5 @costdev
17 months ago

  • Keywords dev-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to reported-upstream
  • Status changed from new to closed

That's great @hbhalodia, thanks! I'll close this ticket as reported-upstream.

For those reading this ticket later, please continue to the issue on the Gutenberg repository.

Note: See TracTickets for help on using tickets.