1

What I'm trying to do is to optimize the way to store data in my wp_postmeta table.

I need to save multiple relations between posts, and my starting situation was simply this:

add_post_meta($parent_id, 'post_child', $child_id);

In that way I needed to use a database row for each relation.

Considering that the same parent can be associated to multiple children, I was trying to figure it out what could be a good array configuration, and I got to something like this (but I'm still not sure is the best way):

array(
    array(
        parent => 121,
        child => 122
    ),
    array(
        parent => 121,
        child => 122
    ),
    array(
        parent => 121,
        child => 123
    ),
    ...
);

Then, I tried with this code:

if ($post_relations = get_post_meta($book_id, 'post_relations', true)) {

    $post_relations[] = array("parent" => $parent_id, "child" => $child_id);

    update_post_meta($book_id, 'post_relations', $post_relations);

} else {

    $post_relations[] = array("parent" => $parent_id, "child" => $child_id);

    add_post_meta($book_id, 'post_relations', $post_relations);

}

But the result I get in the meta_value field seems to be different to the result I was expecting:

a:2:{
i:0;a:2:{s:6:"parent";i:1;s:5:"child";i:510;}i:1;a:2:{s:6:"parent";i:510;s:5:"child";i:511;}
}
supadema
  • 123
  • 1
  • 2
  • 11
  • You don't show the code for `update_post_meta` or `add_post_meta` which seem to be the problematic functions. –  Jun 16 '16 at 15:48
  • I don't understand what you mean, they are Wordpress functions... Which code should I show? – supadema Jun 16 '16 at 16:31
  • Sorry, I'm not a Wordpress expert. I looked at the [add_post_meta documentation](https://codex.wordpress.org/Function_Reference/add_post_meta). You seem to be calling it correctly. The result you show seems to have a parent with id of 1 and child with id of 510, then another parent with id of 510 and a child with id of 511. Maybe you should add your expected result to your question? –  Jun 16 '16 at 17:35
  • The result I expected is the array that you can see in my post as second block of code. By the way, after reading your comment, with a fresh mind I compared again my expected result with the array that I get in the database, and I start to think that the final result is correct... Now I should just understand if it is also convenient! – supadema Jun 17 '16 at 08:35

2 Answers2

0

The WordPress functions update_post_meta() and add_post_meta() both require serializable data;

update_post_meta ( int $post_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )

$meta_valueMust be serializable if non-scalar

update_post_meta()

It looks like this in the database because those functions serialize() the array you're passing it.

Therefore you if you were accessing the data outside of the get_post_meta() function you would have to unserialize() the data.

Rhetorical
  • 183
  • 12
0

I think you should save the relations as one relation per one post meta - saving the relations in the array would make DB queries very difficult.

So, either save your relations as ie post_relations_0, post_relations_1 etc, or, even better, if you plan to do some queries, save the relations in separate table.

user1049961
  • 2,656
  • 9
  • 37
  • 69
  • Saving the relations as one relations per one post meta I guess would be the easiest solution, cause is what I'm already doing and is working fine for me. My only worry is that if I got a really high number of relations to save, what is finally the best performance solution? Many db rows that contain a single value or less db rows that contain a big array of values? – supadema Jun 17 '16 at 08:15
  • 1
    As said, querying the values in array will be difficult - your best bet is one row per one item. I'd go with a separate table with columns id, parent_id, child_id. That way you can easily query anything you need. – user1049961 Jun 17 '16 at 09:51