Custom Column in CPT admin table not updated after Quick Edit save
I have a CPT named company
with custom fields url
(text input) and partner
(checkbox).
Custom columns are shown properly with correct content in CPT admin table. Quick fields are also shown and properly populated for each row.
When the Update button is clicked in Quick Edit the post_meta are properly updated, values saved in database, but column’s content is not updated by Ajax. If I refresh the page the column is displayed correctly but the whole point of this Quick Edit is to avoid a page refresh. Client will not know his changes were saved and will keep quick-editing and loose data. What am I missing?
Custom columns:
add_filter(
'manage_company_posts_columns',
function () {
return [
'cb' => true,
'title' => _x( 'Title', 'column name' ),
'partner' => 'Partner',
'date' => __( 'Date' ),
'last_updated' => __( 'Last Updated' ),
];
}
);
Custom columns content
add_action(
'manage_company_single_posts_custom_column',
function ( $column, $post_id ) {
if ( 'last_updated' == $column ) {
$post_modified = get_post_field( 'post_modified', $post_id );
if ( ! $post_modified ) {
$post_modified = '' . __( 'undefined', 'fs' ) . '';
}
echo __( 'Modified', 'fs' ) . ' <br/>' . date( 'Y-m-d', strtotime( $post_modified ) );
}
if ( 'partner' == $column ) {
if ( get_post_meta( $post_id , 'partner' , true ) !== '0' ) {
echo '<a href="'.get_post_meta($post_id , 'url' , true ) .'"><span class="partner">Partner Company</span></a>';
} else {
echo 'Not a partner company.';
}
}, 10, 2
);
Quick edit fields
add_action(
'quick_edit_custom_box',
function ( $column_name, $post_type ) {
if ( $post_type != 'company' ) return;
switch( $column_name ) :
case 'partner': {
wp_nonce_field( 'company_cpt_quick_edit', 'my_nonce' );
?>
<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<div class="inline-edit-group wp-clearfix">
<label class="alignleft">
<span class="title">Company URL</span>
<input type="text" name="url" value="">
</label>
<label class="alignleft">
<input type="checkbox" name="partner">
<span class="checkbox-title">Partner Company</span>
</label>
</div>
</div>
</fieldset>
<?php
break;
}
endswitch;
}, 10, 2
);
Save Post action
add_action(
'save_post',
function ( $post_id ){
if ( empty( $_POST ) ) return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id;
if ( !wp_verify_nonce( $_POST['my_nonce'], 'company_cpt_quick_edit' ) ) return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
if ( isset( $post->post_type ) && $post->post_type == 'revision' ) return $post_id;
if ( isset( $_POST['url'] ) ) {
update_post_meta( $post_id, 'url', $_POST['url'] );
}
if ( isset( $_POST['partner'] ) ) {
update_post_meta( $post_id, 'partner', '1' );
} else {
update_post_meta( $post_id, 'partner', '0' );
}
}
);
JavaScript
jQuery(function($){
var wp_inline_edit_function = inlineEditPost.edit;
inlineEditPost.edit = function( post_id ) {
wp_inline_edit_function.apply( this, arguments );
var id = 0;
if ( typeof( post_id ) == 'object' ) {
id = parseInt( this.getId( post_id ) );
}
if ( id > 0 ) {
var specific_post_edit_row = $( '#edit-' + id ),
specific_post_row = $( '#post-' + id ),
url = $( '.column-partner', specific_post_row ).find('a:first').attr('href'),
partner = false;
if( $( '.column-partner', specific_post_row ).find('span.partner').length !== 0 ) partner = true;
$( ':input[name="url"]', specific_post_edit_row ).val( url );
$( ':input[name="partner"]', specific_post_edit_row ).prop('checked', partner );
}
}
});
Leave an answer
You must login or register to add a new answer .