2017-10-04 77 views
0

我在WordPress中使用了多個自定義帖子類型。 每個用戶都可以將公司資料添加爲稱爲「公司」的特定帖子類型。 如果用戶有公司資料,他可以添加更多內容,如工作,活動等。全部定義爲特定的自定義帖子類型。WordPress:刪除一個帖子後的所有帖子已被刪除

用戶也有可能刪除自己的公司資料。 如果他這樣做了,那麼用戶的所有其他帖子也應該被刪除(普通帖子除外),並且該URL應該被重定向(301)到主頁。

我在WP文檔(https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post)中找到了操作「delete_post」。但我不知道如何解僱它。

有沒有人有提示?

編輯:見我的回答如下

回答

1

我找到了解決方案。問題是行動'delete_post'。 我把它改成基於郵政狀態轉變「trash_to_publish」:https://codex.wordpress.org/Post_Status_Transitions

現在一切工作正常。

/** 
* Deletes all posts from author of company profile. 
*/ 
function delete_all_posts_from_author($post_id) { 


    global $post; 
    $id = $post->ID; 

    // Only trigger if post type is "company" 
    if (get_post_type($id) == "company") { 

     $author_id = $post->post_author; 

     $posts_from_author = get_posts(
      array(
       'posts_per_page' => -1, 
       'post_status'  => 'publish', 
       'post_type'   => array('event','job'), 
       'author'   => $author_id, 
       'fields'   => 'ids', // Only get post ID's 
      ) 
     ); 

     foreach ($posts_from_author as $post_from_author) { 
      wp_trash_post($post_from_author, false); // Set to False if you want to send them to Trash. 
     } 
    } 


} 

add_action('publish_to_trash', 'delete_all_posts_from_author', 10, 1); 

作爲獎勵,我可以使用的功能再度用戶發佈的所有帖子,如果我取消刪除公司簡介。

/** 
* Untrash posts if company profile is untrashed 
*/ 
function untrash_all_posts_from_author($post_id) { 


    global $post; 
    $id = $post->ID; 

    if (get_post_type($id) == "company") { 

     $author_id = $post->post_author; 

     $posts_from_author = get_posts(
      array(
       'posts_per_page' => -1, 
       'post_status'  => 'trash', 
       'post_type'   => array('event','job'), 
       'author'   => $author_id, 
       'fields'   => 'ids', // Only get post ID's 
      ) 
     ); 

     foreach ($posts_from_author as $post_from_author) { 
      wp_untrash_post($post_from_author); // Set to False if you want to send them to Trash. 
     } 
    } 


} 

add_action('trash_to_publish', 'untrash_all_posts_from_author', 10, 1); 

希望有所幫助。如果我犯了一個錯誤,請讓我知道。

編輯:我已經改變了說法wp_delete_post()wp_trash_post()因爲wp_delete_post()僅適用於本地的帖子,頁面和附件。來自@rarst的大回答在這裏:https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888

+0

看起來不錯:)唯一的是wp_untrash_post()只有一個參數,但這是堅實的。 –

+0

啊,謝謝你的提示。我會改變它:) – Cray

+0

與@ DanielH.J相同。說 - 但對於'wp_trash_post()',@Cray。它只需要1個參數,即post_id。我認爲你對'wp_delete_post()'可能會感到困惑? –

1

喜歡的東西:

// Add action trigger 
add_action('delete_post', 'delete_other_custom_posts', 10); 

function delete_other_custom_posts($post_id) { 
    global $wpdb; 
    $post = get_post($post_id); 

    // Only trigger if post type is "company" 
    if ($post->post_type == "company") { 
     $author_id = $post->post_author; 

     // Find other custom posts' ids by the same user 
     $posts_to_delete = $wpdb->get_col(
      $wpdb->prepare(
       "SELECT ID FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_author = %d AND post_type IN (<put your custom post types' slugs here>)", 
       $author_id 
      ), ARRAY_A 
     ); 

     if ($posts_to_delete) { 
      foreach ($posts_to_delete as $post_delete_id) { 
       // Delete the custom post 
       wp_delete_post($post_delete_id); 
      } 
     } 

     // Redirect 
     wp_redirect(<page you want to redirect to>); 
     exit; 
    } 
} 
+0

我會試試。謝謝! – Cray

+0

嗨,我已經添加了像這樣的post-type slugs:「post_type IN('event','job')」,但不幸的是它不工作?! – Cray

+0

也許這是因爲函數wp_delete文章不適用於自定義文章類型? WP文檔中提到「刪除或刪除帖子,附件或頁面」。 – Cray

0

這是可能的。 公司是郵政類型,所以剩餘的內容將是分類,所以當你刪除後有元,分類不會被刪除。

<?php 
add_action('admin_init', 'codex_init'); 
function codex_init() { 
    add_action('delete_post', 'codex_sync', 10); 
} 

function codex_sync($pid) { 
    global $wpdb; 
    if ($wpdb->get_var($wpdb->prepare('SELECT post_id FROM .'$wpdb->prefix.'postmeta WHERE post_id = %d', $pid))) { 
     $wpdb->query($wpdb->prepare('DELETE FROM '.$wpdb->prefix.'postmeta WHERE post_id = %d', $pid)); 
    } 
} 
?> 
+0

謝謝,但其餘的內容也是一種帖子類型不是一個分類 – Cray