• Resolved stabilimenta

    (@stabilimenta)


    Hi,
    I used CMB2 as a helper for a plugin I made. When originally made it, I put a series of 10 “file” type meta fields. I am now overhauling the plugin and would like to move this large amount of “file” data into a single “file_list” type field.

    I downloaded my DB so I could look at how the data is being stored for a file_list.
    Some of it I can understand:

    (2466, 768, '_cmb_file_list', '
    	a:2:{
    		i:770; //image ID
    		s:86:"https://www.mydomain.com/wp-content/uploads/2018/05/xxx.jpg";
    		i:721; //image ID
    		s:89:"https://www.mydomain.com/wp-content/uploads/2013/07/yyy.jpg";
    	}
    '),

    I understand how to use update_post_meta, and I understand how to get the ID and the URL for the photos saved in the older “file” CMB field types.

    In the above data, I’m not sure what the “a:2” and the “s:86” and the “s:89” signify.

    Can you advise me how I could use update_post_meta or another function to move image data from a series of “file” objects to a single “file_list”?

    I appreciate it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    So to dissect and clarify that blob of data, you’ll want to understand what serialization is. I’ll provide a basic understanding, but don’t take me as final word.

    Starting from the very start, just for completenes sake.

    2466 is the row ID for your postmeta table, 768 is the post ID that this meta data is for, ‘_cmb_file_list” is the meta key, and the rest is the meta value.

    Here’s where the serialization starts. The “a:2” portion indicates that the value stored is an array, with 2 indexes. thus the “a:2”. If it had one index, it’d be “a:1”, if it had fifty, “a:50”.

    The brackets indicate the start of the value contained in that array. The “i:770” is two parts. The “i” refers to that being the array index, and the 770 is the index’s numeral value. In this case, the 770 comes from the attachment ID for the image selected. Very useful. It’d be the same as doing $thing[770] = 'something'; Now, the value that is stored in that slot, is the next line. For a hard example, $thing[770] = "https://www.mydomain.com/wp-content/uploads/2018/05/xxx.jpg"; The “s:86” is the next key part. The “s” means that the value stored is a string, and the 86 is how many characters are being stored. If you counted it out, the url would equal 86 characters, from start to end. That comprises the first index of the two in the stored array. It repeats with the 2nd one, and you get your stored values. As the string values change in length, so will those values after the “s:”

    So, now that we have the rundown of that, fetching and manipulating. get_post_meta/update_post_meta handle the serialization for you, both to and from a “serialized state”. You shouldn’t need to worry about editing this much, though it’s easy to need to (like if changing domain names).

    if you were to do $thing = get_post_meta( 768, '_cmb_file_list', true ); you should get a standard array back, with the values outlined above. You could then add your own values to them and update, or you could iterate over them for display, or manipulation in preparation for the new storage location. Really up to you what you want to do with the data, and using WordPress’ functions will save you some headache from having to deal with the raw database column values.

    Thread Starter stabilimenta

    (@stabilimenta)

    Thank you so much for taking the time to explain that. Really nice!

    Here’s the code I came up with, in case it’s useful to others:

    function pp_convert_meta() {
    	global $pp_prefix;
    	$portprojects = get_posts(array('numberposts'=>-1,'post_type'=>'project_portfolio')); //my CPT is project_portfolio
    
    	foreach( $portprojects as $post ) : setup_postdata($post); 
    		// I have 10 photo "file" fields to move for each project_portfolio post, 
    		// so I set up a loop
    		// Each field id was an incremented number _cmb_photo_1, _cmb_photo_2, etc
    		$i = 1; 
    		while ( $i <= 11 ) {
    			//get image (attachment) ID
    			$this_img_id = get_post_meta($post->ID, '_cmb_photo_'.$i.'_id', true);
    			$this_img_url = get_post_meta($post->ID, '_cmb_photo_'.$i, true);
    			
    			$pp_image_array = array();
    
    			if ($this_img_id && $this_img_url) {
    			
    				$pp_image_array[$this_img_id] = $this_img_url;
    				
    				// copy file meta to file_list meta
    				update_post_meta($post->ID, $pp_prefix . 'file_list', $pp_image_array);
    			}
    			
    			delete_post_meta($post->ID, '_cmb_photo_'.$i);
    			delete_post_meta($post->ID, '_cmb_photo_'.$i.'_id');
    			$i++;
    		} //endwhile
        endforeach;
    }
    
    // you can fire the function from anywhere
    pp_convert_meta();
    • This reply was modified 6 years, 6 months ago by stabilimenta. Reason: forgot to add the $i++;
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

    Thread Starter stabilimenta

    (@stabilimenta)

    Arg! I had the reinstantiation of the array in the wrong place! Try this:

    function pp_convert_meta() {
    	global $pp_prefix;
    	$portprojects = get_posts(array('numberposts'=>-1,'post_type'=>'project_portfolio')); //my CPT is project_portfolio
    
    	foreach( $portprojects as $post ) : setup_postdata($post); 
    		// I have 10 photo "file" fields to move for each project_portfolio post, 
    		// so I set up a loop
    		// Each field id was an incremented number _cmb_photo_1, _cmb_photo_2, etc
    		$pp_image_array = array();
    		$i = 1; 
    		while ( $i <= 11 ) {
    			//get image (attachment) ID
    			$this_img_id = get_post_meta($post->ID, '_cmb_photo_'.$i.'_id', true);
    			$this_img_url = get_post_meta($post->ID, '_cmb_photo_'.$i, true);
    			
    			if ($this_img_id && $this_img_url) {
    			
    				$pp_image_array[$this_img_id] = $this_img_url;
    				
    				// copy file meta to file_list meta
    				update_post_meta($post->ID, $pp_prefix . 'file_list', $pp_image_array);
    			}
    			
    			delete_post_meta($post->ID, '_cmb_photo_'.$i);
    			delete_post_meta($post->ID, '_cmb_photo_'.$i.'_id');
    			$i++;
    		} //endwhile
        endforeach;
    }
    
    // you can fire the function from anywhere
    pp_convert_meta();
    • This reply was modified 6 years, 6 months ago by stabilimenta.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘programmatically insert data into file_list’ is closed to new replies.