Getting Number Field in WP Grid Builder Block Filter

Support General Getting Number Field in WP Grid Builder Block FilterResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37037
    goninski@gmail.com[email protected]
    Participant

    Hello

    I created several custom blocks in WP Grid Builder to output values in cards the way I needed. It works fine except for number fields. For this field only the value of the first post is shown (same value in all posts).

    See code:

    
    add_filter( 'wp_grid_builder/blocks', 'go_register_wpgb_block_price' );
    
    function go_render_wpgb_block_price() {
    
    	$object = wpgb_get_object();
    
    	if ( ! isset( $object->post_type ) ) {
    		return;
    	}
    
        // price fields
        $go_price = rwmb_meta( 'fg_sale_price', $object );
        $go_price_info = rwmb_meta( 'fg_sale_info', '', $object ); 
        $go_term_list = get_the_term_list( $object, 'status-sale' );
    
        echo $go_price . ' ' .  $go_price_info . ' ' . $go_term_list;
    
    }

    The one which is not working is $go_price. I know I should place an argument in the middle like for the other fields. But what to put? If I put '' no value will be retrieved, neither with 0, nothing works and in this documentation https://docs.metabox.io/fields/number I couldn't find anything which could help in this case.

    Thanks in advance for a hint
    Kind regards
    François

    #37047
    Long NguyenLong Nguyen
    Moderator

    Hi François,

    If the helper function rwmb_meta() gets the field fg_sale_info value successfully, it should work for the field fg_sale_price. And the code should be

    $go_price = rwmb_meta( 'fg_sale_price', '', $object );

    Or you can try to use the WordPress function get_post_meta() to get the post meta (field value)

    $go_price = get_post_meta( $object, 'fg_sale_price' );

    Get more details on the documentation https://developer.wordpress.org/reference/functions/get_post_meta/

    #37136
    goninski@gmail.com[email protected]
    Participant

    Hi Long

    Thanks for your reply.

    I found the reason: I needed to add ->ID at the end, so

    '$go_price = rwmb_meta( 'fg_sale_price', '', $object>ID );'

    But I don't know why it was working with normal text fields.
    However, now it's working fine 🙂

    Thanks and regards
    François

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.