I have created a custom meta box to display a dropdown menu that an author can use to select a featured ad for a post, this works fine when the post is published straight away but when it is scheduled, the meta box ad never displays (but if you go back into the post and click update, the ad then appears).
/* Meta Boxes */
add_action( 'add_meta_boxes', 'es_metabox_post_ads_create' );
function es_metabox_post_ads_create()
{
//create a custom meta box
add_meta_box( 'es_metabox_post_ads', 'Post Ads', 'es_metabox_post_ads_function', 'post', 'side', 'low' );
}
function es_metabox_post_ads_function( $post )
{
$eirepanel_inline_ads_options = get_option('eirepanel_inline_ads_options');
$select_post_ads = get_post_meta( $post->ID, '_select_post_ads', true ); ?>
<select name="select_post_ads">
<option style="min-width: 120px;">Please Select</option>
<?php for($i=0; $i<count($eirepanel_inline_ads_options['eirepanel_inline_ads_options_name']); $i++): ?>
<?php
$selected = '';
if($select_post_ads == $eirepanel_inline_ads_options['eirepanel_inline_ads_options_name'][$i])
{
echo $selected = 'selected';
echo $eirepanel_inline_ads_options['eirepanel_inline_ads_options_name'][$i];
}
sort($eirepanel_inline_ads_options['eirepanel_inline_ads_options_name']); ?>
<option style="min-width: 120px;" <?php echo $selected; ?> >
<?php echo $eirepanel_inline_ads_options['eirepanel_inline_ads_options_name'][$i]; ?>
</option>
<?php endfor; ?>
</select>
<?php
}
//hook to save the meta box data
add_action( 'save_post', 'boj_mbe_save_post_ads_meta' );
function boj_mbe_save_post_ads_meta( $post_id )
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
update_post_meta( $post_id, '_select_post_ads', strip_tags( $_POST['select_post_ads'] ) );
}
Jurre Hanema answers:
I believe the save_post action is also called when a post status transitions from scheduled to published. Because in that case $_POST is of course empty the metadata gets removed.
So I have a feeling a simple check before you update the post metadata like this may solve the issue:
function boj_mbe_save_post_ads_meta( $post_id )
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if(isset($_POST['select_post_ads']))
update_post_meta($post_id, '_select_post_ads', strip_tags( $_POST['select_post_ads'] ) );
}
Keith Donegan comments:
Bingo, that makes sense! Thank you
Keith Donegan comments:
How do I pay you? lol
Jurre Hanema comments:
I believe you should use the "Vote?"-link underneath your first post.
Keith Donegan comments:
Done, get it ok?
Utkarsh Kukreti answers:
Please post the code you're using.
Keith Donegan comments:
Hey, is this enough?
Utkarsh Kukreti comments:
There must be some more code. Something that contains 'add_post_meta'.
Keith Donegan comments:
Sorry, missed that block, updated.
Armando Jaleo answers:
Hi Keith:
Please Do you can give me more information, I don't understand "(but if you go back into the post and click update, the ad then appears)"
You can check: to save and publish a post straight away and edit and change the date of publish.
Greetings!
Keith Donegan comments:
Hi Armando.
I can indeed save and publish the post and the ad appears, but my client has alot of authors that need to schedule posts and have then displayed over time.