Support Forum
Support › Meta Box Group › Remove Cloned Field ConditionalResolved
This may take some customizing but I wanted to see if there was a way that cloned items that use the datetime field could automatically be removed on page load or page submission when a particular field has went past a date set.
I am using your extensions to try and create a post scheduling system that uses start and end date/time fields. However, I worry that if they aren't manually deleted it will get rather confusing and may even end up looking cluttered.
Was wondering if you could point me in the right direction to conditionally wipe out cloned groups after an expire date has passed. Even if this would take any custom scripting outside of what Meta Box supports. I just want to see if this use case would be possible before getting too far down the road with trying to use Meta Box for this feature.
Hi,
I think you can do that by looping through the meta box definition and remove the fields when a condition happens. The idea is similar to this documentation. Please try it.
Thanks Anh. Been looking into it but struggling to get anything to work.
$meta_boxes[] = array(
'id' => 'featured_position_1',
'title' => __( 'Featured Position 1', 'alphamedia-blacklab' ),
'settings_pages' => 'alphamedia-featured-content',
'tab' => 'featured_items',
'fields' => array(
array(
'id' => 'featured_items_group_1',
'type' => 'group',
'clone' => true,
'fields' => array(
array(
'name' => __( 'Featured Post', 'alphamedia-blacklab' ),
'id' => 'featured_item_1',
'type' => 'post',
'post_type' => 'any',
'field_type' => 'select_advanced',
'columns' => 4,
),
array(
'name' => __( 'Schedule Start', 'alphamedia-blacklab' ),
'id' => 'featured_item_1_schedule_start',
'type' => 'datetime',
'js_options' => array(
'timeFormat' => 'hh:mm',
),
'columns' => 4,
),
array(
'name' => __( 'Schedule End', 'alphamedia-blacklab' ),
'id' => 'featured_item_1_schedule_end',
'type' => 'datetime',
'js_options' => array(
'timeFormat' => 'hh:mm',
),
'columns' => 4,
),
),
),
),
);
That's the setup for the meta box. Basically every field has just a post, start date, and end date. My goal is to allow the user to schedule posts to appear in a featured section and have it remove end dates that have passed and sort by dates as well. Didn't know how flexible it would be but using Meta Box plugin and extensions really helps to get all the fields put in place. I guess i'm just struggling to gain access to the actual repeater fields that are appearing on the page and there values that are stored.
function edit_meta_boxes() {
foreach ( $meta_boxes as $k => $meta_box ) {
// Edit "Address Info" meta box
if ( isset( $meta_box['id'] ) && 'featured_position_1' == $meta_box['id'] ) {
// Loop through all fields
foreach ( $meta_box['fields']['fields'] as $j => $field ) {
// Add description for "Street" field
if ( 'Featured Post' == $field['name'] ) {
$meta_boxes[$k][$j]['name'] = 'Change Name As Test';
}
}
}
}
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'edit_meta_boxes', 20 );
Hi,
If an user selects:
- Post A from Feb 6 to Feb 10
- Post B from Feb 8 to Feb 12
Then after Feb 6, the post A is removed from the selection (in the admin area)? Is that what you want?
Correct. Basically I am trying to use meta box to create a settings page that allows users to schedule posts that will be featured in 4 featured positions on the homepage. Each position is a separate meta box and within that meta box is a group that contains the post, start date and end date.
My goal would be 3 things.
1.) Order the post entries by start dates.
2.) Delete posts where the current date is past the end date.
3.) Diss-allow users from selecting posts in the same position with the same dates scheduled. (Can probably handle this with Javascript when they are entering dates i'd assume.)
Numbers 1 and 2 would make me have to have access to the actual meta values for each repeated group.
I appreciate you helping me with this. Your plugin and extensions are very useful.
I got it. In this case, the problem is handling the "saved meta value". I'd suggest using WP cron to perform a regular check on the meta value (posts). When a condition occurs (date is passed), then update the meta value.
This is the pseudo code:
register_activation_hook(__FILE__, 'prefix_activation');
function prefix_activation() {
if (! wp_next_scheduled ( 'prefix_daily_check' )) {
wp_schedule_event(time(), 'hourly', 'prefix_daily_check');
}
}
add_action('prefix_daily_check', 'prefix_update_posts_daily');
function prefix_update_posts_daily() {
$now = time();
$option = get_option( 'featured_position_1' );
foreach ( $option as $key => $value ) {
if ( $time > strtotime( $value['featured_item_1_schedule_start'] ) ) {
unset( $option[$key] );
}
}
update_option( 'featured_position_1', $option );
}
Thanks. I think that could work. Instead of a cron could I just hook into a certain WordPress action that checks to see if it's the settings page on each admin page load? That way it's ordering or deleting them on every page load if needed?
That works but it affects your performance as the action "check-update" runs on every request to your website. This triggers the query to the database. Running only on your settings page reduces the load. But that also means the value is updated only when users visit the settings page, so it's kind of manual reset.