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

core/post-title not showing the correct title in dynamic block when used outside loop with postId context #68369

Open
3 of 6 tasks
ivarsm opened this issue Dec 29, 2024 · 3 comments · May be fixed by #68431
Open
3 of 6 tasks
Assignees
Labels
[Block] Post Title Affects the Post Title Block [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@ivarsm
Copy link

ivarsm commented Dec 29, 2024

Description

core/post-title block is a dynamic block and uses render_block_core_post_title method to render title.

When creating dynamic blocks, we can pass postId and postType on a parent block providesContext. This would then mean that core blocks such as post-title, post-featured-image, post-excerpt etc are scoped to a particular post outside loop.

Issue

$block->context['postId']

is used within the method to retrieve permalink for the post, but get_the_title() is used without passing $block->context['postId'] as a parameter. this causes an issue where the title is rendered for the current global $post and permalink for post from context postId.

Desired outcome

$block->context['postId'] should be used for the $title as well to ensure that the right title is retrieved.

This would ensure that core/post-title is scoped to a correct post same as other post related core blocks.

Step-by-step reproduction instructions

  1. Create any dynamic block that allows inner blocks.
  2. in block.json add attributes and providesContext
    "attributes": { 
        "postId": {
            "type": "integer",
            "default": 2319 // any existing post id
        },
        "postType": {
            "type": "string",
            "default": "post"
        },
},
    "providesContext": {
        "postId": "postId",
        "postType": "postType"
	},
  1. render.php (dynamic block callback)
<?php
$blockAttrs = get_block_wrapper_attributes();
?>
<div <?php echo $blockAttrs ?>>
    <?php echo $content ?>
</div>
  1. edit.js
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

function Edit({ attributes, setAttributes }) {
  const blockProps = useBlockProps();
  const innerBlocksProps = useInnerBlocksProps();

  return (
    <div {...blockProps}>
      <div {...innerBlocksProps} />
    </div>
  );
}

export default Edit;
  1. Add core/post-title as inner block.
    The post title in edit page will be scoped correctly, and will be scoped to global $post on the front end.

Screenshots, screen recording, code snippet

No response

Environment info

WordPress - 6.7.1
Active theme - Twenty Twenty-Five
Any browser
Viewing on Mac OS, but the problem is not device specific

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

  • Yes

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

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@ivarsm ivarsm added the [Type] Bug An existing feature does not function as intended label Dec 29, 2024
@rinkalpagdar
Copy link
Contributor

Hello @ivarsm
I am able to reproduce the issue. In the editor, the title is displayed properly, but on the front end, no title is showing.

@Mamaduka Mamaduka added the [Block] Post Title Affects the Post Title Block label Dec 30, 2024
@yogeshbhutkar
Copy link
Contributor

yogeshbhutkar commented Dec 31, 2024

Hi @ivarsm,

Thank you for raising this issue. I believe this is more of an enhancement than a bug. As noted in this comment, the core/post-title block is expected to be used within a Query Loop.

That said, there have been similar requests for enhancement (as mentioned in the linked comment).

Previously, the context’s post ID was removed from the get_the_title() function call to address an issue where changes made to the core/post-title block weren’t reflected in the preview screen.

Reintroducing the post ID with get_the_title() would likely reintroduce the same problem, where preview updates fail to reflect changes made to the block. A potential solution could be to dispatch post title updates directly, instead of using setTitle from useEntityProp. Implementing this with a debounce could address both issues, making the block more reactive and responsive.

I'll be creating a PR that:

  1. Uses context's post ID, instead of the Global Post ID.
  2. Dispatches the changes made on post-title with an added Debounce.

These changes should tackle both the issues and should let the users use the block outside the scope of Query Loop block.

@0x8sksr
Copy link

0x8sksr commented Dec 31, 2024

Pass postId to get_the_title(): Modify the render_block_core_post_title method to use $block->context['postId'] for both get_the_title() and get_permalink():

php
`function render_block_core_post_title($attributes, $content, $block) {
if (!empty($block->context['postId'])) {
$post_id = $block->context['postId'];
$title = get_the_title($post_id);
$permalink = get_permalink($post_id);
} else {
$title = get_the_title();
$permalink = get_permalink();
}

return sprintf('<a href="%s">%s</a>', esc_url($permalink), esc_html($title));

}`

Fallback Handling: Ensure the code gracefully falls back to the global $post if postId is not provided.

Verify Context: Confirm that the parent block correctly provides postId via providesContext.

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 [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
5 participants