AdvancedCustomFields / AdvancedCustomFields/acf
ACF Gutenberg Blocks preloaded post_id
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
I think I found a bug when working with ACF blocks and preloaded post IDs.
Inside the bloc render callback, I'm using new WP_query, but the get_field(); function defaults to the ACF_block ID.
This is my code which is pretty basic.
function block_parfait_collection( $block ) {
$align_class = $block['align'] ? 'align' . $block['align'] : '';
echo '<div class="outer-block '. $align_class . '">';
$query=array(
'post_type' => 'collections',
'posts_per_page' => 10,
'order' => 'DESC',
'orderby' => 'date',
);
$previews = new WP_Query($query);
if($previews->have_posts()) {
while ($previews->have_posts() ) : $previews->the_post();
echo 'test 1';
echo get_field('muse');
echo 'test 2';
echo get_field('muse', get_the_ID(), true);
endwhile;
}
echo '</div>';
}
With this I can already see that get_field(); on "test 1" but would on "test 2" ( with get_the_ID(); )
By trying to debug I found the acf_get_valid_post_id(); in advanced_custom_fields_pro/includes/api-helpers.php
and more precisely this bit of code
$preload = apply_filters( "acf/pre_load_post_id", null, $post_id );
if( $preload !== null ) {
// MY TEST START
echo 'preloaded: ' . $preload;
echo '<br>actual_id :' . get_the_ID();
//MY TEST END
return $preload;
}
With this "test code" I could see that the preloaded "current post id" was block_5cb469a88eca2 (the block id) instead of 2450 (get_the_ID);
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the block render callback with WP_Query and compare get_field() with get_field('muse', get_the_ID(), true). Start in advanced_custom_fields_pro/includes/api-helpers.php at acf_get_valid_post_id() and the acf/pre_load_post_id filter, then verify that the queried post ID is used instead of block_5cb469a88eca2. Done when the reported mismatch no longer occurs for the shown example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, wordpress
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100