[wp-trac] [WordPress Trac] #57295: post-title block uses `get_the_permalink()` function with `post_id` as a parameter

WordPress Trac noreply at wordpress.org
Thu Dec 8 09:25:36 UTC 2022


#57295: post-title block uses `get_the_permalink()` function with `post_id` as a
parameter
-------------------------+-----------------------------
 Reporter:  hbhalodia    |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  minor        |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 **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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57295>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list