Support Forum
Support › Meta Box Include Exclude › Include/Exclude for new/existing posts - Not saving dataResolved
Hello,
As stated in the previous post (link bellow) I succeeded to show one metabox in the New Post page and other metabox in the Edit Post page.
The problem is that the data (meta) is not saved in the database when adding new posts even the metabox fields are displayed correctly.
Can you please advice ?
Thank you, Clement
Hi ClementN,
You can use Conditional Logic in this case. Just add
'visible' => array('original_post_status', '!=', 'publish'),
When you want to show meta box when post is not published, and vice versa.
In case you want to show post on specify post status like publish
, draft
... just put the original status to the value of conditional logic, like so:
'visible' => array('original_post_status', 'auto-draft'),
For example:
add_filter( 'rwmb_meta_boxes', function( $meta_boxes )
{
$meta_boxes[] = array(
'title' => 'Media',
'visible' => array('original_post_status', '!=', 'publish'),
'fields' => array(
array(
'name' => 'URL',
'id' => 'tanurl',
'type' => 'text',
),
)
);
return $meta_boxes;
} );
Best regards
Tan Nguyen
My need is to have 2 different metaboxes (in terms of design and functionality) for new and editing page.
For new page, we have only a few fields and for editing page we have a lot more using tabs, this time.
So, the fields from the "New" will be in the "Editing, too .
This is why the conditional hide is not so useful because it will create 2 different metaboxes containing same field ids.
But I found a solution and please let me know if you see any problem using it.
I still use include/exclude for the 2 metaboxes BUT I use identical id for each one.
That way, during the saving process, Metabox.io will use the fields id from the second MB which will be the same as the ones inserted by the user in the New Page MB .
For me, this seems to be the problem initially (not verified into the code). During the saving process, MB is recreating the metabox to save it.
But during that step.. it seems like for MB the New Page condition is false as it is not on that stage anymore. So it generate the Metabox B with its fields for saving ...
But Metabox B was not displayed so it has no field to be saved..
Using my solution ( same ID for both metaboxes) it seems like I trick the process to consider the fields values from the New Post screen as a Metabox for a non-New Post screen.
I hope I was clear with my explanations..