最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - How to output and alert message when updating a post

programmeradmin1浏览0评论

i want to prevent a duplicate entry for a CPT, so i'm querying the db to find if the record already exists and return if get_result() is true

$query = 'SELECT * FROM `wp_postmeta`WHERE `issue_magazine_id`=' . 
$issue_id . ' AND `issue_number`=' . $_POST['issue_number'];
$result = $wpdb->get_results($query);
if($result) return;

the query is fired when the the user try to save the CPT

How in wordpress can i send alert/error messages in the edit post form?

i want to prevent a duplicate entry for a CPT, so i'm querying the db to find if the record already exists and return if get_result() is true

$query = 'SELECT * FROM `wp_postmeta`WHERE `issue_magazine_id`=' . 
$issue_id . ' AND `issue_number`=' . $_POST['issue_number'];
$result = $wpdb->get_results($query);
if($result) return;

the query is fired when the the user try to save the CPT

How in wordpress can i send alert/error messages in the edit post form?

Share Improve this question asked Jun 17, 2019 at 3:48 Raul Magdalena CatalaRaul Magdalena Catala 113 bronze badges 2
  • Which edit post form is it; on what page and in what hook? – Sally CJ Commented Jun 17, 2019 at 5:50
  • It is on the standart edit form for the post items, i hook the save_post action – Raul Magdalena Catala Commented Jun 17, 2019 at 7:10
Add a comment  | 

1 Answer 1

Reset to default 0

I have developed functionality which is check duplicate value for specific post meta value exist or not on update post. when user update data and if find any record then give a error message in admin dashboard(see attached screenshot). you can use below code according your requirement.

class Check_duplicate_entry {
  public function __construct(){
   add_action( 'save_post', array( $this, 'save_post' ) );
   add_action( 'admin_notices', array( $this, 'admin_notices_callback' ) );
  }

   public function save_post( $post_id ) {
    global $wpdb;
    $issue_id = $_POST['issue_magazine_id'];
    $issue_number = $_POST['issue_number'];
    $query = "SELECT $wpdb->posts.ID  FROM $wpdb->posts  INNER JOIN wp_postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )  INNER JOIN $wpdb->postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  ( $wpdb->postmeta.meta_key = 'issue_magazine_id' AND $wpdb->postmeta.meta_value = $issue_id ) 
  AND 
  ( mt1.meta_key = 'issue_number' AND mt1.meta_value = $issue_number )
)";
    $result = $wpdb->get_results($query);

    if($result && $post_id != $result[0]->ID){

    // Add your query var if the coordinates are not retreive correctly.
        add_filter( 'redirect_post_location', array( $this, 'add_notice_query_variable' ), 99 );
    }
    else
    {
        if ( !empty( $_POST[ 'issue_magazine_id' ] ) ) {
            update_post_meta( $post_id, 'issue_magazine_id', $_POST[ 'issue_magazine_id' ]);
        } else {
            delete_post_meta( $post_id, 'issue_magazine_id' );
        }

        if ( !empty( $_POST[ 'issue_number' ] ) ) {
            update_post_meta( $post_id, 'issue_number', $_POST[ 'issue_number' ]);
        } else {
            delete_post_meta( $post_id, 'issue_number' );
        }
    }
  }

  public function add_notice_query_variable( $location ) {
   remove_filter( 'redirect_post_location', array( $this, 'add_notice_query_variable' ), 99 );
   return add_query_arg( array( 'issue_magazine_id' => $_POST['issue_magazine_id'],'issue_number' => $_POST['issue_number'] ), $location );
  }

  public function admin_notices_callback() {
   if ( ! isset( $_GET['issue_magazine_id'] ) && ! isset( $_GET['issue_number'] ) ) {
     return;
   }
   ?>
   <div class="error notice">
      <p><?php esc_html_e( 'Issue ID : '.$_GET['issue_magazine_id'].' and Issue Number : '. $_GET['issue_number'].' already updated in other post, Please try different!', '' ); ?></p>
   </div>
   <?php
  }
}
$Check_duplicate_entry = new Check_duplicate_entry();

May be this will helpful to you. Please let me know if this will helps you!

Thank you!

发布评论

评论列表(0)

  1. 暂无评论