Support Forum
Support › Meta Box Group › if ! empty $groupvariable not working
Hey guys!
I've got the same issue I had a few months ago. I'm not sure we ever really fixed it.
Querying if a group is empty or not doesn't seem to work as described in the docs.
The code I'm using:
// Coverage Shortcode
function epi_publication_coverage_shortcode() {
ob_start();
// Press Coverage
$coveragegroup = rwmb_meta( 'epi_pubcoverage' );
if ( ! empty( $coveragegroup ) ) {
echo 'NOT EMPTY';
}
else {
}
return ob_get_clean();
}
add_shortcode('show_publication_coverage','epi_publication_coverage_shortcode');
Displays "NOT EMPTY" on all posts, even if they don't have any coverage.
Is the group saving a blank field somewhere? Should I be querying on a checkbox or something instead?
Code used to register group:
// Add Coverage Meta Boxes
$meta_boxes[] = array(
'title' => 'Press Coverage',
'post_types' => array('post', 'report', 'analysis', 'guest-blog'),
'context' => 'normal',
'priority' => 'low',
'desc' => '(drag to reorder)',
'fields' => array(
array(
'id' => 'epi_pubcoverage',
'type' => 'group',
'clone' => true,
'sort_clone' => true,
'fields' => array(
array(
'name' => 'Title',
'id' => $prefix . 'pubcoveragedesc',
'type' => 'text',
'desc' => 'Title or description for link',
),
array(
'name' => 'URL',
'id' => $prefix . 'pubcoverageurl',
'type' => 'text',
'desc' => 'Link to coverage',
),
array(
'name' => 'Date',
'id' => $prefix . 'pubcoveragedate',
'type' => 'date',
'desc' => 'Date of event coverage',
),
),
),
),
);
return $meta_boxes;
And all plugins have been updated to latest versions.
Confirmed. The default white space is somehow being saved as a populated field when the post is updated.
I renamed the group and the if !empty worked. I updated a post, leaving these fields blank, and it stopped working.
Even if these fields are left blank, the empty fields still register as being content and if !empty returns true.
I'm gonna use a checkbox to hide this section for now and query that instead.
I understand. I thought I did this in the previous version but looks like not. I will fix it so empty
work.
Thanks for noticing the bug.
No worries, Anh! Hope you figure it out 🙂 Not a problem to bypass it with a checkbox as long as people are aware of the issue.