I'm using the group of functions below to create a checkbox in the Quick Edit area of each post in the post listing at wp-admin/edit.php
that saves a value to a custom field called headline_news
.
The issue is when the checkbox is checked, Quick Edit saves the value, but it won't save again when I uncheck a checked box. Via PHPMyAdmin, the custom field value changes in the database when saving a checked box, but does not change when unchecking a box and saving.
The first three functions are fairly standard to add a column, echo contents of the column and printing the checkbox. I'm guessing the issue with not saving an unchecked box must be in the saving function on in the Javascripts.
// Add column to posts listing
add_filter( 'manage_post_posts_columns', 'add_columns' );
function add_columns( $columns ) {
$columns['headline_news'] = 'Headline news';
return $columns;
}
// Echo contents of custom field in column
add_action( 'manage_posts_custom_column', 'columns_content', 10, 2 );
function columns_content( $column_name, $post_id ) {
if( $column_name == 'headline_news' ) {
$headline_news = get_post_meta( $post_id, 'headline_news', true );
echo $headline_news ;
}
}
// Print checkbox in Quick Edit
add_action( 'quick_edit_custom_box', 'quick_edit_add', 10, 2 );
function quick_edit_add( $column_name, $post_type ) {
printf( '
<input type="checkbox" name="headline_news" class="headline_news"> %s',
'Headline news position'
);
}
// Save checkbox value
add_action( 'save_post', 'qedit_save_post', 10, 2 );
function qedit_save_post( $post_id, $post ) {
// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) )
return $post_id;
// verify quick edit nonce
if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
return $post_id;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' )
return $post_id;
// Save only the custom field
$custom_fields = array( 'headline_news' );
foreach( $custom_fields as $field ) {
if ( array_key_exists( $field, $_POST ) )
update_post_meta( $post_id, $field, $_POST[ $field ] );
}
}
// Javascript functions to set/update checkbox
add_action( 'admin_footer', 'quick_edit_javascript' );
function quick_edit_javascript() {
global $current_screen;
if ( 'post' != $current_screen->post_type )
{
return;
}
?>
<script type="text/javascript">
function checked_headline_news( fieldValue )
{
inlineEditPost.revert();
jQuery( '.headline_news' ).attr( 'checked', 0 == fieldValue ? false : true );
}
</script>
<?php
}
add_filter( 'post_row_actions', 'expand_quick_edit_link', 10, 2 );
function expand_quick_edit_link( $actions, $post ) {
global $current_screen;
$data = get_post_meta( $post->ID, 'headline_news', true );
$data = empty( $data ) ? 0 : 1;
$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="';
$actions['inline hide-if-no-js'] .= esc_attr( 'Edit this item inline' ) . '"';
$actions['inline hide-if-no-js'] .= " onclick=\"checked_headline_news('{$data}')\" >";
$actions['inline hide-if-no-js'] .= 'Quick Edit';
$actions['inline hide-if-no-js'] .= '</a>';
return $actions;
}
Edit 1/23/17
Changing part of the save function to this now works:
// Save only the custom field
if (isset($_POST['headline_news'])) {
update_post_meta( $post_id, 'headline_news', 'yes' );
} else {
delete_post_meta( $post_id, 'headline_news' );
}
It allows me to save and delete the custom field from quick edit. But, when I edit a post in the full post editor - edit text, change category, etc. - the custom field is deleted. So why is that happening? How do I keep saving in the full post editor from changing this meta field?
And: why are there two checkbox fields in quick edit?:
$_POST[ $field ]
does anyway; the save function is cribbed from another example of using Quick Edit. I can probably delete the foreach, too, as there is only one custom field. I'm using a checkbox because I eventually need to change the save function to delete_post_meta on save if the box is unchecked; I need to query posts on the existence of the custom field, not its content, the end result being so I don't have to add that custom field to thousands of posts already in a database.