Support Forum
I'm working on a site that will be utilizing The Events Calendar/Event Tickets/WooCommerce for event registration and e-commerce. Meta Box is being used to add some extra meta data to the event "venue" which is a custom post type.
Defining and entering the meta data works fine. When attempting to display the meta data in the front-end I'm running into some odd behavior. When the "venue" itself is displayed, rwmb_the_value() appears to return correct meta data (although for multi-valued fields it returns an array which I wasn't expecting; simple to extra the "value" portion of the array in PHP, though).
However, when "venue" meta data is attempted to be extracted while the event itself is being displayed, I'm getting some weird results. The same PHP code that returns correct extracted "venue" meta data for the venue display no longer works. rwmb_the_value() returns null!
I'm wondering if rwmb_the_value() is expecting some global WP post ID variable to be set. In the situation where I'm having problems, the front-end is displaying an "event" (a custom post with ID 1, say) while displaying additional data about the "venue" (another separate custom post with ID 2, say). So, data is being displayed from TWO custom posts with two different post IDs.
Happy to give more details/clarification if needed...
Hi,
Thanks for asking this. There are 2 internal helper functions rwmb_get_value()
and rwmb_the_value()
which are used in rwmb_meta()
function. They're not 100% stable since v4.8.0 so I haven't put them in the docs yet.
Briefly, rwmb_get_value
returns the raw value stored in the post meta, while rwmb_the_value
returns (or outputs) the *meaningful* value in the frontend. Meaningful = option label for select box, post title for post field, a colorful dot for color field, etc.
Both functions accept the same parameters as rwmb_meta
. Parameters also share the same meaning.
rwmb_get_value( $field_id, $args = array(), $post_id = null )
rwmb_the_value( $field_id, $args = array(), $post_id = null, $echo = true )
They also use $post_id
if you want to get meta value from a specific post.
In your case if the result goes unexpected, please try setting up the $post_id
. Anyway, there was also a topic reporting the same problem happened because of wrong post ID. But I couldn't find out why
I am passing a value for $post_id
. In one scenario, I get correct results (although, unexpectedly in array format) while in another scenario I do NOT get correct results. Exact same code, exact same passed parameters.
This points to something in the rwmb_the_value()
function not behaving correctly (or something in my hosting environment being corrupted). I have created a separate site (database and WordPress files from scratch) to test whether or not something in my WordPress install is causing this and found the same results.
Again, my guess is that rwmb_the_value()
(or other functions or WordPress APIs that it calls) is relying on some global WordPress value being set. As I attempted to explain previously, in the scenario where rwmb_the_value()
does not work is a scenario where I'm attempting to retrieve meta data for a post which is NOT the one being displayed (i.e., I'm including data and meta data from a separate post in the post that WordPress is displaying in a normal manner).
Maybe you're right. I need to check that again. Can you tell me which field type are you using?
This is for a radio list. The odd thing is that it works in one situation (albeit, with an array as the return value) and not at all in another with the same theme and plugins.
Just found the bug and pushed a fix on Github. Can you please test that?
Perfect! Thanks for the quick fix, much appreciated!