How can I update the post instead of Insert
- This topic has 4 replies, 3 voices, and was last updated 1 year, 6 months ago by
John.
-
AuthorPosts
-
March 11, 2019 at 7:41 PM #13662
calpaq
ParticipantHi Support,
I have a requirement of saving meta data for CPT, the challenge is save data to the existing post if the same post title is already there and do not create a different post. so can you guide me that which hooks will trap these events and how to corealate wp hooks with metabox. eagerly waiting for your reply.Thanks & Regards,
CalpaqMarch 12, 2019 at 10:32 AM #13681Anh Tran
KeymasterHi Calpaq, how do you save posts? I mean do you run a code to save posts or use Meta Box plugins?
If you need to hook after Meta Box save post data, you can just use 'rwmb_after_save_post' hook, or
save_post
(with priority > 10). And then useupdate_post_meta
to update meta data to an existing post of your choice.March 12, 2019 at 12:43 PM #13686calpaq
ParticipantHi Tran,
Thanks for replying,
This is all relaated with wordpress admin
Let me tell you that I am not using any custom code to save posts and use meta box plugins. In my case I am using custom post type, so I have registered post type and by default its menu is added to wp admin so from there adding/updating/deleting posts. WordPress doesn't validate for post title and it can add same title with different Id. What I want is to validate post title ( i.e. check if post exists and if found retrieve that post and update that instead of creating new post with again same title ). I am using some metabox fields for this post type and now I want to check before insert that if any post exist. So actually I do not want create/insert any new post (if exist ), retrieve the ID and save these meta fields to that existing post, Can you help me in saving these fields with existing post when its already there.
Thanks & RegardsMarch 13, 2019 at 9:39 AM #13706Anh Tran
KeymasterI got it. I think the hard part in this situation is detect if there's any existing post with the same title. I'm afraid I don't have any that code available.
Once you get that post (with post ID), you can use
update_post_meta
function to manually update fields.October 18, 2023 at 7:54 AM #43550John
ParticipantI am researching this topic and have a use case where I have a datetime field.
I would like to update an existing post with a new date.
I would like to use the jQuery datetime UI for the user to select the date.So, there is an existing post with an date/time set. I want the user to update the field using the date time picker. Right now, the front end form only created new post it does not have the ability to update post meta I don't think.
I can easily update post meta using the Meta Box functions to update, but i want to bring in the date/time picker.
Any solution for this use case?
thank you!
-
AuthorPosts
- You must be logged in to reply to this topic.