I made a custom post type and on saving its additional data I want to check if I a published post exists by its name. It works alright if there is, but I would like to throw some notice if article is not found.
$post_exists = $wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name="" . $_POST["article_name'] . "' AND post_type="post"", 'ARRAY_A');
if($post_exists)
update_post_meta($id, 'article_name', strip_tags($_POST['article_name']));
else
???
I noticed there is http://codex.wordpress.org/Class_Reference/WP_Error but I dont think that is what I want since debugging is set to false?. Error could be anything – some usual notice would be fine, but even simple javascript alert could be good.
Currently it tells me that the post is saved with green lights which doesnt seem right
2 Answers
You can use admin_notices
hook
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices
For example:
function ravs_admin_notice() {
?>
<div class="error">
<p><?php _e( 'Article with this title is not found!', 'my-text-domain' ); ?></p>
</div>
<?php
}
on publish_{your_custom_post_type}
hook
http://codex.wordpress.org/Post_Status_Transitions
function on_post_publish( $ID, $post ) {
// A function to perform actions when a {your_custom_post_type} is published.
$post_exists = $wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name="" . $_POST["article_name'] . "' AND post_type="post"", 'ARRAY_A');
if($post_exists)
update_post_meta($id, 'article_name', strip_tags($_POST['article_name']));
else
add_action( 'admin_notices', 'ravs_admin_notice' );
}
add_action( 'publish_{your_custom_post_type}', 'on_post_publish', 10, 2 );