• Hi,

    I am writing a plugin for my own site which allows user submitted content to a custom post type with custom fields.

    I want to check for duplicates and have looked at two ways to do this.

    1 – Run when a post is saved, to see if there is a duplicate in my post_meta table, if there is return false for wp_insert_post_data

    however when I do this, it returns to the add post page for the custom post type with an error but still saves some data

    2 – Save the post and once saved check for the duplicate and then delete the post and inform user of duplicate.

    I can’t seem to find which action hook I would use for scenario 2.

    Save_post wants a post id or post data, and I can’t see just how to use this.

    My function should be very simple along the lines of

    if ($post_type == 'media_link') {
    		$query = "SELECT count(ID) FROM {$wpdb->prefix}media_links WHERE LOWER(link)='".strtolower($_POST["cctm_media_link"])."' and ";
    		$count = $wpdb->get_var($query);
    		if ($count > 1 ){
    		 echo "there is a duplicate";
    		}
    else
    {
    echo "there are no duplicates";
    }
    		return $data;

    So really I just need to run that query to see if there are other items, if there are then I need to do something with that like wp_delete()

    also once I run the delete, how do I return a message to the user telling them that it was a duplicate?

    Dan

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You could hook transition_post_status and only act when the status is ‘new’. You need the post data because you need the post ID in order to delete the duplicate. The action doesn’t want post data, it is passing it to your function.

    You could just end the deletion process with a die() message about the duplicate. Better would be to load a new page with the message, and then send the user to somewhere meaningful on acknowledgement, maybe post-new.php. Best would be to add the message to the add post page display, no idea how on this one though.

Viewing 1 replies (of 1 total)
  • The topic ‘Which hook do I use for the following scenarios?’ is closed to new replies.