この記事は2年以上前に書かれたものです。
情報が古い可能性があります。
情報が古い可能性があります。
検証バージョン
- WordPress 3.8.1
ゴミ箱へ移動後に取り消しを選んでも、取り消しが行われるのは親記事だけです。そこまでは考慮していないのでご了承ください。
カスタム投稿の親記事をゴミ箱に移動した時に子記事もゴミ箱に移動する
my_post_type の部分はカスタム投稿タイプ名に書き換えてください。
<?php
/**
* カスタム投稿の親記事をゴミ箱に移動した時に子記事もゴミ箱に移動する
*/
add_action( 'trash_my_post_type', 'trash_child_post' );
function trash_child_post( $post_id ) {
// 子ページの取得
$children = get_children( array('post_type' => 'my_post_type', 'post_parent' => $post_id ) );
// 子ページをゴミ箱へ
if ( is_array ( $children ) ) {
foreach( $children as $child ) {
wp_trash_post( $child->ID );
}
}
}
親ページをゴミ箱に移動した時に子ページもゴミ箱に移動する
<?php
/**
* 親ページをゴミ箱に移動した時に子ページもゴミ箱に移動する
*/
add_action( 'trash_page', 'trash_child_page' );
function trash_child_page( $post_id ) {
// 子ページの取得
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array( 'post_type' => 'page' ) );
$children = get_page_children( $post_id, $all_wp_pages );
// 子ページをゴミ箱へ
if ( is_array ( $children ) ) {
foreach( $children as $child ) {
wp_trash_post( $child->ID );
}
}
}