• Resolved gutterboy333

    (@gutterboy333)


    Hello,

    I’m trying to work out if you can sort products by last edited date in admin? I see a “published date”, but not an edit date – is this possible?

    Also, comfortable doing this as an SQL query within phpmyadmin if not possible in admin.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    I suggest creating an extra column for this on the product page.

    Step 1, the header

    /**
     * Add edited date column on product page (header)
     */
    function add_edited_date_product_column_header( $columns ) {	
        //add column header
        $columns['edited_date'] = __( 'Edited date', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_edited_date_product_column_header', 10, 1 );

    Step 2, the content

    /**
     * Add edited date column on product page (content)
     */
    function add_edited_date_product_column_content( $column, $postid ) {
        // add column content
        if ( $column == 'edited_date' ) {	
            // Get product object
            $product = wc_get_product( $postid );
    		
            // Get product date modified
            $date_modified = $product->get_date_modified();
    		
            // Echo output
            echo 'Modified' . '<br><span title="' . $formatted_date = date( 'Y/m/d h:i:s a', strtotime( $date_modified ) ) . '">' . $formatted_date = date( 'd F Y', strtotime( $date_modified ) ) . '</span>';
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_edited_date_product_column_content', 10, 2 );

    Step 3, make the column sortable

    /**
     * Make the edited date column on the product page sortable
     */
    function edited_date_product_column_sortable( $columns ) {
        $columns['edited_date'] = 'edited_date';	
    
        //To make a column 'un-sortable' remove it from the array
        //unset($columns['date']);
    	
        return $columns;
    }
    add_filter( 'manage_edit-product_sortable_columns', 'edited_date_product_column_sortable', 10, 1 );

    Step 4, orderby. This not yet finished! you may try to find the query yourself as a challenge, good luck!

    /**
     * Orderby edited_date (product)
     */
    function edited_date_product_column_orderby( $query ) {
        if( ! is_admin() )
            return;
    
        $orderby = $query->get('orderby');
    
        // edited date, product
        if( $orderby == 'edited_date' ) {
            //$query->set( 'orderby', '' );
        }
    }
    add_action( 'pre_get_posts', 'edited_date_product_column_orderby' );

    Regards

    Thread Starter gutterboy333

    (@gutterboy333)

    Thanks very much for the code; it’s not something I needed to add long term, just needed to check it this once as I needed to restore a backup and needed to check if any products had been modified after the backup date.

    However, I went into the DB and worked out how to do it in phpmyadmin – for anyone else wondering:

    SELECT * FROM wp_posts WHERE post_type='product' order by post_modified DESC

    Cheers!

    • This reply was modified 4 years, 10 months ago by gutterboy333.
    • This reply was modified 4 years, 10 months ago by gutterboy333.

    nice code should i put it in function.php ??

    i put all there but it tells me smthing wrong and when i delet the last code nothing happen

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort products by last edited date in admin?’ is closed to new replies.