-
rawhasan
Hi,
I have a
tour
custom post type which is showing posts from theevent
custom post type that are associated with thetour
posts using an ACF Relationship Field.The code is inside a query loop to get the events associated with the current tour. The
tour-length
custom field is assigned on the currenttour
post outside the query loop where the code is.How to retrieve the value of the
tour-length
custom field then?add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'format-tour-date' === $block['attrs']['className'] ) { $event_date_data = get_post_meta( get_the_id(), 'tour-start-date', true ); $event_date_format = ! empty( $event_date_data ) ? date_i18n( 'F j, Y', strtotime( $event_date_data ) ) : ''; $tour_length = get_post_meta( get_the_id(), 'tour-length', true ); var_dump($tour_length); $block_content .= $tour_length; $block_content = str_replace('TOUR DATE', $event_date_format, $block_content); } return $block_content; }, 10, 2 );
The output of the var_dump is:
string(2) "14" string(0) "" string(0) ""
The output on the block is:
October 19, 2024
Expected output on the block:
October 19, 2024 14
How can I retrieve the value
14
and assign to$tour_length
?Thanks for your help!
-
rawhasan
After some trial and error, I was able to fetch the tour-length custom field’s value correctly and can output it on the screen inside a h2 element to test. But while trying to output it on the block, the tour-length does not show up. How can I display it in this format in the block:
October 19, 2024 - 14 Days
?add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'format-tour-date' === $block['attrs']['className'] ) { // Get the tour start date $event_date_data = get_post_meta( get_the_id(), 'tour-start-date', true ); $event_date_format = ! empty( $event_date_data ) ? date_i18n( 'F j, Y', strtotime( $event_date_data ) ) : ''; // Get the global post and the tour length field value global $post; $tour_length = get_post_meta( $post->ID, 'tour-length', true ); echo "<h2>" . $tour_length . "</h2>"; // Ensure both date and tour length are formatted if ( ! empty( $event_date_format ) && ! empty( $tour_length ) ) { $formatted_content = $event_date_format . ' - ' . $tour_length . ' Days'; } else { // If tour length is missing, just show the event date $formatted_content = $event_date_format; } // Replace the placeholder with the new content $block_content = str_replace('TOUR DATE', $formatted_content, $block_content); } return $block_content; }, 10, 2 );
This code is now showing only the date as this in the block:
October 19, 2024
– not this part- 14 Days
.Thanks for your help!
-
Hi there,
In your first code, try to swap the position of these 2 lines, and add the “Days” after the block content.
$block_content .= $tour_length; $block_content = str_replace('TOUR DATE', $event_date_format, $block_content);
So change it to:
$block_content = str_replace('TOUR DATE', $event_date_format, $block_content); $block_content .= '-'.$tour_length.' Days';
-
rawhasan
It is not working, unfortunately.
I’ve updated the code as this:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'format-tour-date' === $block['attrs']['className'] ) { $event_date_data = get_post_meta( get_the_id(), 'tour-start-date', true ); $event_date_format = ! empty( $event_date_data ) ? date_i18n( 'F j, Y', strtotime( $event_date_data ) ) : ''; $tour_length = get_post_meta( get_the_id(), 'tour-length', true ); echo "<h2>" . $tour_length . "</h2>"; $block_content = str_replace('TOUR DATE', $event_date_format, $block_content); $block_content .= ' -'.$tour_length.' Days'; } return $block_content; }, 10, 2 );
The
echo
is displaying the tour-length alright as this: https://prnt.sc/oIdptS1lL-yABut not rendering in the block: https://prnt.sc/bzmfO-sA5z7E
Also, the part outside the
str_replace
looks like it has fallen outside the block, as it is not following the style of the block. -
The code isn’t the one I was mentioning, I mean modify your 1st PHP code in this topic.
This:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'format-tour-date' === $block['attrs']['className'] ) { $event_date_data = get_post_meta( get_the_id(), 'tour-start-date', true ); $event_date_format = ! empty( $event_date_data ) ? date_i18n( 'F j, Y', strtotime( $event_date_data ) ) : ''; $tour_length = get_post_meta( get_the_id(), 'tour-length', true ); var_dump($tour_length); $block_content = str_replace('TOUR DATE', $event_date_format, $block_content); $block_content .= '-'.$tour_length.' Days'; } return $block_content; }, 10, 2 );
And I saw another
render_block
filter in your child theme is targeting the sameformat-tour-date
class, to avoid conflicting, you should only be using only onerender_block
filter for one block. -
rawhasan
Hi Ying,
I’ve fixed this myself after two days of trial and error, thanks!
Can you please just tell me what is the best way to retrieve a custom field value of the parent post (tour) from this render_block filter which is running inside another custom post (event) related with a ACF Relationship Field? It looks like the value is retrieved once and then getting overwritten when the query loop is running.
Thanks!
-
Hi there,
render_block
is a core function of WordPress that has its own filter hook:https://developer.wordpress.org/reference/hooks/render_block/
It exists in all well coded blocks, and simply allows you to filter the front end rendered content of any block. It doesn’t do anything smart or connect things. So you need some code that retrieves related field data to which you can return that result inside the
render_block
callbackHere is a commented PHP Snippet that explains how to return a related posts custom field inside it:
function custom_render_related_event_block($block_content, $block) { // abort if in admin if (is_admin() || wp_is_json_request()) { return $block_content; } // find block with a class of <code>your-custom-class</code> if ( ! empty( $block['attrs']['className'] ) && 'your-custom-class' === $block['attrs']['className'] ) { // Get the current event's ID $event_id = get_the_ID(); // Get the related tours (assuming 'tours' is the name of the ACF relationship field) $related_tours = get_field('tours', $event_id); // Initialize output variable $related_content_output = ''; // Loop through related tours to get their related-content field if ($related_tours) { foreach ($related_tours as $tour) { // Assuming 'related-content' is the name of the field in the tour post type $related_content = get_field('related-content', $tour->ID); if ($related_content) { $related_content_output .= '<div class="related-content">' . esc_html($related_content) . '</div>'; } } } // if there is realted content; return it. if ($related_content) { return $related_content_output; } } return $block_content; } // Hook into the 'render_block' filter add_filter('render_block', 'custom_render_related_event_block', 10, 2);
- You must be logged in to reply to this topic.