Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Getting a custom field value outside the query loop

  • Hi,

    I have a tour custom post type which is showing posts from the event custom post type that are associated with the tour 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 current tour 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!

  • 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';
  • 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-yA

    But 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 same format-tour-date class, to avoid conflicting, you should only be using only one render_block filter for one block.

  • 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 callback

    Here 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);
    
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.