V
V
vasya0922021-09-12 23:57:54
WordPress
vasya092, 2021-09-12 23:57:54

How can I add an object field to a post in Wordpress?

The essence of the question is that there is a post type "Match" and inside this type of post I need to have such a repeatable field as "Goals" and in this field I need to store values ​​like: "Player ID: Number of goals".
The question is, how is it possible to store the values ​​of such a field within a given post type? Is it possible to save objects inside a post? Or, in this case, will it be necessary to create another type of post (goal) and link it to the match?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-09-13
@vasya092

Fields are used to store record data post_meta. It is most reasonable to store a string or an array in this field, but you can experiment with objects too

$competition['Барселона'][] = [
  'time' => '27:01',
  'person' => 'Месси',
];

$competition['ПСЖ'][] = [
  'time' => '32:16',
  'person' => 'Мбаппе',
];

$competition['ПСЖ'][] = [
  'time' => '70:44',
  'person' => 'Кин',
];

update_post_meta( $post_id, '_competition', $competition );

You can get the field value using the functionget_post_meta()
$competition = get_post_meta( $post_id, '_competition', true );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question