Skip Navigation

[Gelöst] How to add post meta when inserting posts with wp_insert_post

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Client wants to add post meta at the same time as inserting posts with the function wp_insert_post.

Solution:
Add the post meta fields as an array using the meta_input parameter.

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters

This support ticket is created vor 6 Jahren, 8 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von mohammadD vor 6 Jahren, 8 Monaten.

Assistiert von: Nigel.

Author
Artikel
#621430

Hi there!

i'm writing a plugin that inserts posts using wordpress wp_insert_post($args) function
as you know there should be a "post_meta" key in the $args array

a have a post type with a costume field group,
i can handle the insertion process, using a wpcf- prefix before field name, and all type of fields works fine except the date-type field.
for example i have a date format like this: "7/8/2017"
i can convert that to time stamp using php DateTime class,
well, how can i assign this timestamp to 'wpcf-myfield' key of post_meta array ?

'wpcf-myfield' => $timeStamp
or
'wpcf-myfield' => $orginalForm

don't in use

#621479

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi there

If you want to add post meta directly using wp_insert_post you add the post meta fields as an array using the meta_input parameter (https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters).

A Types date field is stored as a timestamp, and you can convert dates from a string format to a timestamp using the PHP strtotime function: versteckter Link

I think that is the part you are missing.

#621755

merC