Custom Column in CPT admin table not updated after Quick Edit save
I have a CPT named company
with custom fields url
and partner
.
Custom column is 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 custom 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 lose data. What am I missing?
EDIT: My problem was because of a caching function related to PODS, a framework I am using for custom port_meta and fields. Row was returned after quick edit but values were cached. I overridden caching for these functions and everything worked properly.
Full working code with Quick Bulk Edit
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_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';
}
echo 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=partnerPartner 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=titleCompany URL/span
input type=text name=url value=
/label
label class=alignleft
input type=checkbox name=partner
span class=checkbox-titlePartner 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 for quick edit
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 );
}
}
});
Bulk Edit radio field
add_action(
'bulk_edit_custom_box',
function ( $column_name, $post_type ) {
if ( $post_type != 'company_single' ) return;
switch( $column_name ) :
case 'partner': {
?
fieldset class=inline-edit-col-right
div class=inline-edit-col
div class=inline-edit-group wp-clearfix
label class=alignleft
input type=radio id=partner name=partner class=partner value=0
label for=partnerPartner/label
input type=radio id=non_partner name=partner class=partner value=1
label for=non_partner Non-partner/label
/label
/div
/div
/fieldset
?php
break;
}
endswitch;
}, 10, 2
);
Bulk Edit save function
function company_bulk_edit_save_hook() {
if( empty( $_POST[ 'post_ids' ] ) ) {
die();
}
foreach( $_POST[ 'post_ids' ] as $id ) {
if ( isset( $_POST['partner'] ) ) {
update_post_meta( $id, 'partner', $_POST['partner'] );
} else {
update_post_meta( $id, 'partner', '0' );
}
}
wp_die();
}
add_action( 'wp_ajax_company_bulk_edit_save', 'company_bulk_edit_save_hook' );
AJAX for Bulk Edit
jQuery(function($){
$( 'body' ).on( 'click', 'input[name=bulk_edit]', function() {
$( this ).after('span class=spinner is-active/span');
var bulk_edit_row = $( 'tr#bulk-edit' ),
post_ids = new Array(),
partner = bulk_edit_row.find( '.partner:checked' ).val();
bulk_edit_row.find( '#bulk-titles' ).children().each( function() {
post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
});
$.ajax({
url: ajaxurl,
type: 'POST',
async: false,
cache: false,
data: {
action: 'company_bulk_edit_save',
post_ids: post_ids,
partner: partner
}
});
});
});
Topic quick-edit custom-field custom-post-types Wordpress
Category Web