post-title
block uses get_the_permalink()
function with post_id
as a parameter
#46417
Labels
post-title
block uses get_the_permalink()
function with post_id
as a parameter
#46417
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 inthe_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 filterpost_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 usingget_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#L23Possible Solution:
Need to remove the
post_id
as a parameter fromget_the_permalink()
and use the current WP_Post instance to fetch the required value.Step-by-step reproduction instructions
post-title
block insidequery-loop
block.$post_id
parameter from thepost-title
block, This would be working as expected.Screenshots, screen recording, code snippet
Video Link
Environment info
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
The text was updated successfully, but these errors were encountered: