1) Where I should use wp_delete_post() If I want to first of all to show a message that this post already exist and just then delete this post and stay on this page?
2) How to show red message instead of yellow?

1 Answer
1

Checking if meta values exist

You can use get_posts() (or the WP_Query object) to query all posts which match the post meta you want to save. You will need to specify the post status (i.e. which statuses are you willing to ignore). Below is the untested code to this.

(For completeness I've left in arguments which could be omitted because they are given their default value). I also exlude the current post, in case the post is being updated - since otherwise that post will be returned and we'll think a post already exists with that meta data.

Side remarks

The 'post_terms' etc seem quite generic. I would give them more descriptive and unique names - event more so if being used as key in the post meta table. Also, to avoid conflict you might want to send the data inside an array with a unique name: i.e.

<input type="text" name="myplugin[post_terms]" value=""/>

Then retrieve the data as $_POST['myplugin']['post_terms'] rather than $_POST['post_terms']. You can be reasonably sure that any data inside $_POST['myplugin'] has been over-written by another plug-in. Also you must use nonces to help verify source / intention.

Preventing the post from publishing

You can't prevent a post from being published (unless you use jQuery). However, immediately after a post is published, you can perform a check and, if desired, revert it back to draft status: See my solution to this related question.

Displaying a Custom message

In the linked solution, if a post had been reverted back to draft, I set the message variable to '10' so that the draft message is shown: 'Draft updated....':

Hi Stephen! Thanks for quick response, I updated my question. For example user insert in custom fields values 1) Car 2) Bmw 3) X5 ...so I don't want to save this post if already created a post for 'Car','BMW','X5'. I would be very appreciated if you help me with solution )))
–
user13250Mar 22 '12 at 11:27

Is there a way to don't save a post? I mean when I'm clicking a publish button ....post will saved with status draft, but it will be in database..i'm trying to escape this
–
user13250Mar 23 '12 at 12:52

Almost got what I want. Just one question. At this moment I have a save function , redirect location function and post updated messages function. Where I should use wp_delete_post($id); function? If I use it in my save function -> I can't see the message, if I use it in redirect location function -> I see a white page with message 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?'. Look at updated question please! And also, I want to ask, how to make red message instead of yellow.
–
user13250Mar 23 '12 at 18:52