I am trying to create a custom post type input using the wp_insert_post
function, this custom post type called game
was created using the pods framework and contains a series of custom fields which I try to give values using the meta_input key
as follows
Example using _pods_
prefix
$entry = [
"post_title" => "{$post->post_title} - {$game->number}",
"post_content" => "... {$id}",
"post_type" => "game",
"post_status" => "publish",
"meta_input" => [
"_pods_game_number" => $game->number,
"_pods_game_date" => $game->date,
"_pods_fields" => $fields,
"_pods_league" => $id,
]
];
$game_id = wp_insert_post($entry, true);
Example with no prefix
$entry = [
"post_title" => "{$post->post_title} - {$game->number}",
"post_content" => "... {$id}",
"post_type" => "game",
"post_status" => "publish",
"meta_input" => [
"game_day_number" => $game->number,
"game_day_date" => $game->date,
"fields" => $fields,
"league" => $id,
]
];
$game_day_id = wp_insert_post($entry, true);
For some reason the meta fields are not saved except for the fields
field ... Investigating I read somewhere about using the _pods_
prefix in custom fields but although I can verify that the fields and their correct values are created in the postmeta
table these are not listed in the form. If on the other hand I try to reference the custom fields without the prefix _pods_
they are not created, I confirm this by looking for them in the postmeta
table.
What is the correct way that when creating a custom post type assign values to custom fields created with pods framework?
I appreciate your time
I am trying to create a custom post type input using the wp_insert_post
function, this custom post type called game
was created using the pods framework and contains a series of custom fields which I try to give values using the meta_input key
as follows
Example using _pods_
prefix
$entry = [
"post_title" => "{$post->post_title} - {$game->number}",
"post_content" => "... {$id}",
"post_type" => "game",
"post_status" => "publish",
"meta_input" => [
"_pods_game_number" => $game->number,
"_pods_game_date" => $game->date,
"_pods_fields" => $fields,
"_pods_league" => $id,
]
];
$game_id = wp_insert_post($entry, true);
Example with no prefix
$entry = [
"post_title" => "{$post->post_title} - {$game->number}",
"post_content" => "... {$id}",
"post_type" => "game",
"post_status" => "publish",
"meta_input" => [
"game_day_number" => $game->number,
"game_day_date" => $game->date,
"fields" => $fields,
"league" => $id,
]
];
$game_day_id = wp_insert_post($entry, true);
For some reason the meta fields are not saved except for the fields
field ... Investigating I read somewhere about using the _pods_
prefix in custom fields but although I can verify that the fields and their correct values are created in the postmeta
table these are not listed in the form. If on the other hand I try to reference the custom fields without the prefix _pods_
they are not created, I confirm this by looking for them in the postmeta
table.
What is the correct way that when creating a custom post type assign values to custom fields created with pods framework?
I appreciate your time
Share Improve this question asked Aug 7, 2020 at 2:52 user615274user615274 1237 bronze badges 1 |1 Answer
Reset to default 0I have managed to solve using the add_post_meta function instead of the meta_input
property of the $postarr
argument of the wp_insert_post function, as follows
Instead of this
$entry = [
"post_title" => "Some post title",
"post_content" => "Some post content",
"post_type" => "name_of_custom_post_type",
"post_status" => "publish",
"meta_input" => [
"some_meta_key" => "Some value",
"another_meta_key" => "Another value"
]
];
$post_id = wp_insert_post($entry, true);
I used this
$entry = [
"post_title" => "Some post title",
"post_content" => "Some post content",
"post_type" => "name_of_custom_post_type",
"post_status" => "publish",
];
$post_id = wp_insert_post($entry, true);
add_post_meta($post_id, "some_meta_key", "Some value");
add_post_meta($post_id, "another_meta_key", "Another value");
I do not understand the reason why it has not worked for me, when inspecting the result of $wp_error
I do not get an error, but as I have described in the question when using meta_input
no values are registered in postmeta
table
_
prefix means you want the custom field not to appear in Custom Fields metabox in post edit screen. Both you codes looks fine to me and they both should work. – Abhik Commented Aug 7, 2020 at 3:45