Skip Navigation

[Resolved] How can I add a custom post to my WP site?

This support ticket is created 3 years, 8 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1711767

Dear Sir/Madam,

Every time when a visitor visits my site, I want to add a custom post with a custom field, can I have the code to do that?

Best regards,

Kelvin.

#1711925

I can provide a code snippet that will create a custom post and set a custom field value in that post, but I am not sure of the best way to trigger that code once per visit. The logic for determining "what is a visit" and "who is a visitor" is complex, and there are many factors to consider. It is not something Toolset is designed to accomplish, so there are no Toolset APIs related to "visit"s or "visitors".

To create a custom post and set a custom field value you can trigger code like this:

// Create a post object
$my_post = array(
  'post_title'    => 'The post title',
  'post_type'     => 'post-type-slug',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'meta_input' => array(
    'wpcf-field-slug' => 'field value',
  )
);
 
// Insert the post into the database
wp_insert_post( $my_post );

If the custom field is created in Types, you must use the wpcf- prefix with the field slug from wp-admin. If the field is not created in Types, the wpcf- prefix is not required for the field slug.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.