Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why are post metas are stored as an array instead of sperarate keys #13

Closed
joehoyle opened this issue Jan 25, 2016 · 5 comments · Fixed by #56
Closed

Why are post metas are stored as an array instead of sperarate keys #13

joehoyle opened this issue Jan 25, 2016 · 5 comments · Fixed by #56

Comments

@joehoyle
Copy link

I noticed for some reason, all meta for a single key is stored as a single meta key (serialized array) in the revision meta, this makes calling get_post_meta on a revision return an array-encapsulated data rather than the data you would get from calling get_post_meta on the parent post. I just wondered why that was?

@adamsilverstein
Copy link
Owner

Hmm, I need to look at this more closely, I am seeing this as well.

@adamsilverstein
Copy link
Owner

@joehoyle I pushed a proposed fix for this issue in #27 - if you have a chance can you give it a test? Thanks

@roborourke
Copy link

Hey @adamsilverstein, I'm not sure #27 is the solution as meta data can be stored in any of the following ways:

  1. Single string value
  2. Serialised array / object value
  3. Multiple entries of either of the above with the same key

Using get_post_meta() with $single set to true you'll lose data in scenario 3.

I think at some point some assumptions would have to be made about the data if it's an array so if the response from get_post_meta( $id, $key ) is:

  1. A numbered array with one entry
    1. Store the array value eg add_metadata( 'post', $id, $key, $meta[0] )
    2. When retrieved using get_post_meta( $id, $key ) should be an array with one entry
  2. A numbered array with multiple entries
  3. Loop over the array and call add_metadata( 'post', $id, $key, $item )
  4. When retrieving the metadata you should get an array of the items back

It should be enough to change it in the following way:

$meta_values = get_post_meta( $post_id, $meta_key );

/*
 * Use the underlying add_metadata() function vs add_post_meta()
 * to ensure metadata is added to the revision post and not its parent.
 */
foreach ( $meta_values as $meta_value ) {
    add_metadata( 'post', $revision_id, $meta_key, $meta_value );
}

There are then going to need to be updates to the way revision is restored and also compared I think.

@danielbachhuber
Copy link

Hi @joehoyle

@adamsilverstein
Copy link
Owner

Note: Planning to implement #56 as soon as we can get to expanding tests for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants