-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Query block with shortcode breaks get_the_ID
or get_post
#43053
Comments
I just found:
So are shortcodes executed at a later point then? Are there other ways to do what i am trying to do? |
This looks like a duplicate of #35676, where the solution was given at #35676 (comment) |
Well, kind of. Since a lot of plugins and users use shortcodes still, it might be beneficial to be able to use it in this context. The fix is also straight forward and apparently works or is there a drawback to it I don't see.
This will also prevent adding blank lines arrount the shortcode. |
I see what you are saying and I have previously proposed the same change, albeit to solve a different problem. I still think the issue is a duplicate, just that you have explained it better than the other issue. |
Shure thing. Good call. It was just trubling to me to close this issue in favor of an already closed one, that does not solve the underlying problem. |
Just want to point out that this also applies to shortcodes in block content such as paragraphs as well, not just the shortcode block. While I understand that blocks are the future, shortcodes are currently the only way to inline dynamic content. Example: 1 December 2022 at 9 am-10 am |
I need this solution too. |
This doesn't work for me. Problem remains the same. :-( |
A simpler solution to the logic that overrides the dynamic rendering logic for the shortcode block is to implement a hook for the filter that's run at the end of each block.
This is how I've chosen to implement the fix for my Fizzie theme. |
thanks, i've had the same problem and this was the only sollution that came close. though for some reason this creates "phantom" paragraphs around the result block a.e. |
Hi there The proposed solutions do not work for me. Either replacing the content of However, I had the |
This was working fine (I think) for a while even in 6.2.2, but today I noticed its not working again. Last time it broke (first solution) was updating from 6.2.1 but now I haven't done anything and still it fails to work. Will try to do a quick research and reply back. @bobbingwide does this still work for you? I can't figure out why it stopped working all of the sudden. |
In case this helps anyone: I tried to downgrade, but found the problem still existed with 6.1.3. However, 6.1.2 worked. I am looking at the diff between the two. There are a few intersting things, but I am short on time right now. When using linux the diff can easily be viewed with:
|
@saemideluxe it looks like it's in |
Right, I read it too now. But I am completely lost about why exactly this change would cause things to stop working X-D |
I think it's because |
fwiw @Bellian‘s theme-level workaround is still working for me. |
For me, it was working in 6.2 and it stopped in 6.2.1 (and 6.2.2) so I had to downgrade |
Theme level workaround |
It also worked for me untill 6.2. My workarround was then to simply replace the shortcode with another shortcode and let it evaluate during the loop..
|
I wrote a quick workaround for ACF here by replacing the default The main issue is that the shortcode block isn't responsible for rendering it's own shortcodes, so the context that's provided to the renderer which would make There's probably a couple of ways this could be resolved, or at least optimised:
|
@lgladdy works great for me, but Im afraid its not the fastest way on server side. I hope soon wordpress will fix this issue. |
This is still very much an issue.. I had been using a hook to call do_shortocde() as @bobbingwide had suggested and this was working fine, but now, perhaps since 6.4, its no longer working. Why is this not a top priority for word press to get this fixed once and for all? Any new workarounds? This has completely killed my site. |
Same here, we are currently stuck on 6.1 and will not be able to
upgrade. A big chunk of our content presentation relies on this now
unusable feature.
Quoting Jason Etzel (2023-11-21 06:08:51)
… This is still very much an issue.. I had been using a hock to call do_shortocde
() as @bobbingwide had suggested and this was working fine, but now, perhaps
since 6.4, its no longer working.
Why is this not a top priority for word press to get this fixed once and for
all?
Any new workarounds? This has completely killed my site.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.*Message ID: <WordPress/
***@***.***>
|
@Bellian Tried this on 6.4.2 but no joy (also tried with do_shortcode()). Does it still work for you? |
Anyone who still gets stuck with this issue can use my plugin Meta Field Block to solve it. My plugin has been referred to as a solution to a similar issue here. Since version 1.2.3, it has native support for running shortcode. You just set the field type to |
Wow, this is nice. Thank you!
|
This is great. Thanks @Mr2P! |
@Mr2P Wish I could say this would help me.. but I'm using pods and have queries
running thru code snippets and those only work by getting the correct post
id.
EDIT: This is working but you seem to be wrapping HTML around the output value.. this is breaking my output as it is HTML but only the opening tag.. I close it later. Is there a way to have it not wrap anything around the output?
|
@etzel42 You could remove the wrapped HTML like this: add_filter( 'render_block', function ( $block_content, $block ) {
if ( 'mfb/meta-field-block' === $block['blockName'] ) {
// Define your regex pattern.
$pattern = '/<div class=\'value\'>(.*?)<\/div>/s';
// Perform the regex search.
preg_match($pattern, $block_content, $matches);
if ( $matches ) {
return $matches[1];
}
}
return $block_content;
}, 10, 2); I have not tested it myself, but I think it should work. |
@Mr2P: Man, this is awesome, you just saved me from total madness. Rebuilding a site with a lot of dynamic stuff and with only native WP blocks, I have to fully rely on shortcodes (back to old school WP), and I'm so shocked that we can't access post ID inside the loop!!! Thanks a zillion! |
@yankiara you are welcome. I'm glad you found it helpful. |
@Mr2P I can't get it working with wrapped content shortcodes:
Am I missing something or is it not supported? |
@yankiara, this works with all types of shortcodes. Could you please provide me with your code? I will investigate your issue. Also, could you post it on the plugin support page? This thread is not about this plugin. |
Solution for WP Shortcodes in Query Loop Post TemplatesThe problemBefore rendering a page, WordPress first generates the output for all blocks without actually printing them. When using a Query Loop, during each "loop cycle" WordPress generates the block code for each loop item using its The solutionI was looking for a solution that doesn't require "hacking" the core of WordPress. It needed to be lightweight, universal, easy to use, and configurable, relying solely on native approaches. Additionally, it had to ensure that everything works as originally intended where this solution isn't needed. For all details, ready to use code and usage guide please check this gist that I've dedicated to this: |
Description
I am curently using the block editor to create a website.
Since I am using custom post types and custom taxonomys and custom meta data, there is currently no way to to display these.
So i included a shortcode to render those relevant informations.
this worked fine for single posts, but as soon as I used this in archives within the content loop,
get_the_ID
orget_post
only return data from the first post in the query. In this case i may find a workaround by just counting how many times this hortcode is called and accesing the id by the$posts
array, but still, this would be awesome if this could be fixed.Step-by-step reproduction instructions
Screenshots, screen recording, code snippet
The "Featured Image" is added by the block editor. There you see the two images.
The text on the side shows the bug. They both have the same ID (marked in red).
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.
Yes
The text was updated successfully, but these errors were encountered: