2013-12-20 135 views
0

我有一個網站使用Meta-Box插件的以下字段的自定義元框。代碼如下 `檢查是否有發佈的自定義元數據的重複Wordpress帖子

$meta_boxes[] = array(
     'title' => 'MLS ID', 
     'pages' => array('property'), 
     'fields' => array(

     array(
        'name' => 'MLS ID', 
        'id' => "IntegratorPropertyID", 
        'desc' => 'MLS: e.g. 240091025-217', 
        'type' => 'text', 
       ), 
     array(
      'name' => 'Test MLS', 
      'id' => "mlsTest", 
      'desc' => 'Test MLS for Duplicate', 
      'type' => 'button', 
     ), 
    ), 
     'validation' => array(
       'rules' => array(
        "IntegratorPropertyID" => array(
       'required' => true 
      ),     
     ), 
     'messages' => array(
         "IntegratorPropertyID" => array(
        'required' => 'MLS is required', 
       ), 
      ) 
     )  
    ); 

現在什麼即時尋找是增加一個「add_action('save_post', 'checkMLS');」功能來檢查MLS號以前所有的CPT財產,以確保它之前沒有被輸入。我使用的代碼是:

function checkMLS($post_id) { 
      $slug = 'property'; 
      if ($slug != $_POST['post_type']) { 
      return; 
       } 
       $mls2 = rwmb_meta('IntegratorPropertyID', 'type=text', $post_id); 
      $args = array('post_type' => 'property'); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post(); 
      $this1 = get_the_ID(); 
      $mls1 = rwmb_meta('IntegratorPropertyID', 'type=text', $this1); 
      if ($mls2 == $mls1) { 
       $my_post = array(
        'ID' => $post_id, 
        'IntegratorPropertyID' => 'DUPLICATE!' 
        ); 
       wp_update_post($my_post); 
       return; 
      } 
      endwhile; 
     } 
add_action('save_post', 'checkMLS'); 

該代碼在functions.php中找到,當我嘗試發佈屏幕變白。調試模式也不提供任何幫助。 :/

我敢肯定我正在編寫一些主要的錯誤。有人能指出嗎?或者讓我指向正確的方向?或者建議完全不同的東西?

感謝 基思

回答

1

確定。首先,你的白頁沒有說明原因,可能是'內存不足'錯誤,或者是php'最大執行時間'錯誤。這在checkMLS()函數的工作方式中從一個主要缺陷中扼殺。缺點是,你真的騎單車通過數據庫中的所有'property'職位。根據數據集的大小,這可能是一個很大的問題,特別是考慮到您正在處理MLS列表。

我的建議:

找出rwmb_meta()功能是如何抓住它的信息。它可能只是get_post_meta()函數的包裝函數,但可能不是。假設是,我建議做以下,我會在評論解釋,以及之後的細節:

// the save_post action runs after a post has been saved/created, and has two parameters 
// param 1: the id of the post 
// param 2: the post object 
function checkMLS($post_id, $post) { 
    // use the post object post_type to determine if this is a property or not. 
    // it will be a lot more reliable 
    if ($post->post_type != 'property') return; 

    // meta_key should be equal to the 'meta_key' field in the wp_postmeta table, for the 
    // id you are trying to check against. your example used IntegratorPropertyID. 
    // again you may want to check rwmb_meta() function to figure out if there is a 
    // 'prefix' or 'suffix' added to this. despite that, it is almost certainly going to 
    // be looking in the wp_postmeta table, so this should work nicely 
    $meta_key = 'IntegratorPropertyID'; 

    // look up the current mls id for this post, which you just saved/created 
    $mls_id = get_post_meta($post_id, $meta_key, true); 

    // lookup in the actual database table for any matching row, that has the same MLS id 
    // that is not this post. 
    global $wpdb; 
    $q = $wpdb->prepare('select post_id from '.$wpdb->postmeta.' where meta_key = %s and meta_value = %s and post_id != %d limit 1', $meta_key, $mls_id, $post_id); 
    $exists = $wpdb->get_var($q); 

    // if it already exists, mark the value as a duplicate 
    if ($exists) update_post_meta($post_id, $meta_key, 'DUPLICATE!'); 
} 

// add your check function late in the actions, at priority 10000 
add_action('save_post', 'checkMLS', 10000, 2); 

從頂部,我們創建兩個參數的回調,因爲save_post動作發送兩個,$post_id$post。由於save_post在之後運行該帖子已保存,因此您已擁有一個包含所有帖子信息的對象($post)。然後,我們可以使用該對象來確定帖子的類型,這比查看$_REQUEST的值更可靠,主要是因爲$post直接從數據庫中提取並傳遞給您。

現在,如前所述,我認爲rwmb_meta()只是get_post_meta()的一種包裝功能。它可能會爲$meta_key添加前綴或後綴,但對rwmb_meta()函數的一些研究應告訴您在將$meta_key傳遞給get_post_meta()函數時如何更改,並且您可以從那裏修改$meta_key。使用正確的$meta_key,我們現在可以獲取剛剛保存的財產的MLS ID。

使用該MLS ID,我們需要在數據庫中進行直接查找,以確定是否存在具有該ID的其他屬性。雖然演示函數的方式可以處理少量的數據,但它無法處理任何數量可觀的屬性。因此需要直接的方法。只需製作一些特殊的SQL,即可在wp_postmeta表中查找任何post_id,其MLS ID等於爲此屬性輸入的那個,而不是此屬性。如果我們發現一個不屬於這個屬性的匹配,那麼這是一個騙局。如果是笨蛋,我們需要將其標記爲「笨蛋」。

請注意,此解決方案根本不會執行任何循環。它不可能循環超過10000條記錄來查找一個dup ID。這是精簡的。它直接在db中查找id,看看是否有dups。

希望這會對您有所幫助,並希望其他人也能找到幫助。我的公司幾乎專門從事WordPress的工作。通過我們多年與WordPress的合作,我們遇到了從超簡單到過於複雜的問題。同樣的問題,在不同的環境下,已經體現在我們的許多客戶身上。這個解決方案很簡單,但很重要,儘管高度定製。然而,它會工作。

+0

感謝這工作就像一個魅力:)對不起,我從來沒有回答 –

相關問題