0

I've been searching for this and all I found was for new columns to create....so...

I have a column named Views which shows the number of views the post has. The column name in admin is tie_post_views and the post meta field is tie_views

I would like to edit and save the values in that field from the Quick Edit post option.

I appreciate your help.

Edit:

I found a new code that "works" but this is adding a new column and i only edited the meta but not quite what i need since i already have the column




     /*
 * New columns
 */
add_filter('manage_post_posts_columns', 'misha_price_and_featured_columns');
// the above hook will add columns only for default 'post' post type, for CPT:
// manage_{POST TYPE NAME}_posts_columns
function misha_price_and_featured_columns( $column_array ) {
 
    $column_array['price'] = 'Price';
    
    // the above code will add columns at the end of the array
    // if you want columns to be added in another place, use array_slice()
 
    return $column_array;
}
 
/*
 * Populate our new columns with data
 */
add_action('manage_posts_custom_column', 'misha_populate_both_columns', 10, 2);
function misha_populate_both_columns( $column_name, $id ) {
 
    // if you have to populate more that one columns, use switch()
    switch( $column_name ) :
        case 'price': {
            echo '$'.get_post_meta( $id, 'tie_views', true );
            break;
        }
        
    endswitch;
 
}
/*
 * quick_edit_custom_box allows to add HTML in Quick Edit
 * Please note: it files for EACH column, so it is similar to manage_posts_custom_column
 */
add_action('quick_edit_custom_box',  'misha_quick_edit_fields', 10, 2);
 
function misha_quick_edit_fields( $column_name, $post_type ) {
 
    // you can check post type as well but is seems not required because your columns are added for specific CPT anyway
 
    switch( $column_name ) :
        case 'price': {
 
            // you can also print Nonce here, do not do it ouside the switch() because it will be printed many times
            wp_nonce_field( 'misha_q_edit_nonce', 'misha_nonce' );
 
            // please note: the  classes could be:
            // inline-edit-col-left, inline-edit-col-center, inline-edit-col-right
            // each class for each column, all columns are float:left,
            // so, if you want a left column, use clear:both element before
            // the best way to use classes here is to look in browser "inspect element" at the other fields
 
            // for the FIRST column only, it opens  element, all our fields will be there
            echo '
                
                    ';
 
            echo '
                    Price
                    
                ';
 
            break;
 
        }
        
 
    endswitch;
 
}

/*
 * Quick Edit Save
 */
add_action( 'save_post', 'misha_quick_edit_save' );
 
function misha_quick_edit_save( $post_id ){
 
    // check user capabilities
    if ( !current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
 
    // check nonce
    if ( !wp_verify_nonce( $_POST['misha_nonce'], 'misha_q_edit_nonce' ) ) {
        return;
    }
 
    // update the price
    if ( isset( $_POST['price'] ) ) {
        update_post_meta( $post_id, 'tie_views', $_POST['price'] );
    }
 
    
 
}


3
  • This is WP_Posts_List_Table inline_edit. I don't see many hooks there you can use though. You said you know how to create a new column, though? Can't you put the number in that new column?
    – Rup
    Commented Oct 2, 2020 at 12:55
  • the column already exists from the template and is populated with the right values...i just need to make a quick edit to it so i don't have to edit each post
    – Nath
    Commented Oct 2, 2020 at 14:24
  • Oh, sorry, I thought you meant you knew how to make a column in the quick edit view, not a database column.
    – Rup
    Commented Oct 2, 2020 at 14:38

1 Answer 1

-1

Here is my meccano made of pieces found somewhere around. This code is from one of my unfinished and abandoned projects, but it worked (to my personal memory).

Note that I've prefixed the meta name with the underscore (_tie_post_views) to avoid its appearance in Custom Fields metabox on Post Edit page.

You'll be forced to play for a while with the field positioning (may be use JavaScript?) because of flawed Quick Edit and Bulk Edit implementation.

Sorry, I have no possibility to describe and comment the code right now, but I'll try to answer the relevant questions one day. Also, it looks almost self-explanatory.

<?php

///////////////////////// Quick Edit

add_action( 'quick_edit_custom_box', 'tie_display_bulk_quick_edit_postviews', 10, 2 );
add_action( 'bulk_edit_custom_box', 'tie_display_bulk_quick_edit_postviews', 10, 2 );


// Empty form field, data will be populated by JavaScript
function tie_display_bulk_quick_edit_postviews( $column ) {

    if ( 'tie_post_views' === $column ) {

        ?>
        <fieldset class="inline-edit-col-right">
            <div class="inline-edit-col">
                <div class="inline-edit-group wp-clearfix">
                    <label class="alignleft" for="tie_post_views"><span class="title"><?php _e( 'Tie Post Views', 'textdomain' ); ?></span></label>
                    <input type="text" name="_tie_post_views" id="tie_post_views" value="" />
                </div>
            </div>
        </fieldset>
        <?php
    }
}


add_action( 'admin_enqueue_scripts', 'tie_bulk_quick_edit_script' );

function tie_bulk_quick_edit_script() {
    wp_enqueue_script(
        'tie-bulk-quick-edit',
        plugins_url( 'js/tie-bulk-quick-edit.js', __FILE__ ),
        array(
            'jquery',
            'inline-edit-post',
        )
    );
}


add_action( 'save_post', 'tie_save_quick_edit_meta_post_views', 10, 2 );

function tie_save_quick_edit_meta_post_views( $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;
    }

    // Regular post meta update goes here

}

///////////////////////// Bulk Edit
// https://developer.wordpress.org/reference/hooks/wp_ajax_action/
add_action( 'wp_ajax_tie_post_views_bulk_save', 'tie_post_views_bulk_save' );

function tie_post_views_bulk_save() {

    $post_ids = ( isset( $_POST['post_ids'] ) && ! empty( $_POST['post_ids'] ) ) ? $_POST['post_ids'] : null;

    if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {

        if ( isset( $_POST['_tie_post_views'] ) && ! empty( $_POST['_tie_post_views'] ) ) {

            foreach ( $post_ids as $post_id ) {
                update_post_meta( $post_id, '_tie_post_views', $_POST['_tie_post_views'] );
            }
        }
    }

}

And tie-bulk-quick-edit.js (should be completely rewritten, but it subserves):

jQuery(document).ready(function($){

    //Prepopulating quick-edit
    var $inline_editor = inlineEditPost.edit;
    inlineEditPost.edit = function(id){

        $inline_editor.apply(this, arguments);

        var post_id = 0;
        if( typeof(id) == 'object'){
            post_id = parseInt(this.getId(id));
        }

        if(post_id != 0){
            $row = $('#edit-' + post_id);

            tie_post_views = $('#post-' + post_id + ' .tie_post_views').text();
            $row.find('#tie_post_views').val(tie_post_views);
        }
    }

    // Detect 'Enter' key press inside 'Bulk Edit' and click 'Update' button
    // https://stackoverflow.com/a/18160418/
    $('#bulk-edit').keypress(function (e) {
        var key = e.which;
        if(key == 13) {
            $('#bulk_edit').click();
            return false;
        }
    });

    // Live() is obsolete
    // This is workaround to avoid refactoring
    // Think about a better solution
    // https://stackoverflow.com/questions/14354040/jquery-1-9-live-is-not-a-function
    if (typeof jQuery.fn.live == 'undefined' || !(jQuery.isFunction(jQuery.fn.live))) {
        jQuery.fn.extend({
            live: function (event, callback) {
                if (this.selector) {
                    jQuery(document).on(event, this.selector, callback);
                }
            }
       });
    }

    $( '#bulk_edit' ).live( 'click', function(e) {

        var $bulk_row = $( '#bulk-edit' );

        var $post_ids = new Array();
        $bulk_row.find( '#bulk-titles' ).children().each( function() {
            $post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
        });

        var $tie_post_views = $bulk_row.find( 'input[name="_tie_post_views"]' ).val();

        $.ajax({
            url: ajaxurl,
            type: 'POST',
            async: false,
            cache: false,
            data: {
                action: 'tie_post_views_bulk_save',
                _tie_post_views: $tie_post_views
            }
        });
    });
});
2
  • This throws a fatal error, it's unusable.
    – DevSem
    Commented Jan 21, 2022 at 21:55
  • Nothing to fix, works on virgin installation.
    – Max Yudin
    Commented Jan 21, 2022 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.