WordPress.org

Ready to get started?Download WordPress

Forums

Exports and Reports
[resolved] Serialization Arrays (2 posts)

  1. lok1728
    Member
    Posted 1 year ago #

    I have a table with the following:
    s_meta_value with the following array (an array within an array?):
    a:2:{s:9:"post_type";s:4:"post";s:8:"post_ids";s:13:"40,268,18,319";}

    The last value list of numbers are post ID's that I need use to select for the report. How do I go about extracting each of those ID's to use for the WHERE clause?
    Unfortunately I didn't make/create the table, it's from a plugin otherwise I probably would not have serialized those ID's so they could be JOIN and searched.
    I've tried SUBSTRING_INDEX but this doesn't work as the value is "40,268,18,319"

    http://wordpress.org/extend/plugins/exports-and-reports/

  2. Scott Kingsley Clark
    Member
    Plugin Author

    Posted 1 year ago #

    You could try a custom display function to take that value and do what you want to it.

    /**
     * Custom Display Function for Exports and Reports
     *
     * @param string $value The value of the current field
     * @param array $full The values for all of the fields ($another_value = $full[ 'another_field' ])
     * @param string $field The current field name
     * @param WP_Admin_UI $obj The WP_Admin_UI class object (used for the admin screens in Exports and Reports)
     *
     * @return string The value to display in the report
     */
    function your_custom_display_function ( $value, $full, $field, $obj = null ) {
        // Unserialize array
        $unserialized = @unserialize( $value );
        $value = '';
    
        // Make sure $unserialized is an array and post_ids key is set
        if ( is_array( $unserialized ) && isset( $unserialized[ 'post_ids' ] ) ) {
            $ids = explode( ',', trim( $unserialized[ 'post_ids' ] ) );
    
            foreach ( $ids as $k => $id ) {
                $ids[ $k ] = get_the_title( (int) $id );
            }
    
            $value = implode( ', ', $ids );
        }
    
        return $value;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic