/**
 * 投稿（post）の親記事選択ドロップダウンを同一カテゴリー内に限定する
 */

// 1. 親記事選択のクエリ引数に「現在の投稿ID」を潜り込ませる
add_filter('page_attributes_dropdown_pages_args', 'limit_parent_post_by_category_args', 10, 2);
add_filter('quick_edit_dropdown_pages_args', 'limit_parent_post_by_category_args', 10, 2);
function limit_parent_post_by_category_args($args, $post) {
    if ($args['post_type'] === 'post') {
        $args['current_post_id'] = $post->ID; // 判定用にIDをセット
    }
    return $args;
}

// 2. 実際にリストを取得する「get_pages」に割り込み、カテゴリーで記事を再取得して差し替える
add_filter('get_pages', function($pages, $args) {
    // 投稿(post)の親選択プロセスであり、かつIDが特定できている場合のみ実行
    if (isset($args['post_type']) && $args['post_type'] === 'post' && !empty($args['current_post_id'])) {
        
        $current_post_id = $args['current_post_id'];
        $categories = wp_get_post_categories($current_post_id);

        // 記事にカテゴリーが設定されている場合
        if (!empty($categories)) {
            $filtered_posts = get_posts(array(
                'post_type'      => 'post',
                'posts_per_page' => -1,          // 全件
                'category__in'   => $categories, // 同一カテゴリー限定
                'exclude'        => array($current_post_id), // 自分自身は除外
                'post_status'    => array('publish', 'draft', 'pending'), // 公開・下書き含む
                'orderby'        => 'title',
                'order'          => 'ASC',
                'suppress_filters' => false      // 他のフィルターも考慮
            ));

            // もし該当する記事があれば、元の全記事リスト($pages)をこの絞り込みリストに置き換える
            if (!empty($filtered_posts)) {
                return $filtered_posts;
            } else {
                // 該当なしなら空にする（他のカテゴリーの記事を出さない）
                return array();
            }
        }
    }
    return $pages;
}, 10, 2);<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet type='text/xsl' href='https://law-text.com/wp-content/plugins/google-sitemap-generator/sitemap.xsl'?><!-- sitemap-generator-url='http://www.arnebrachhold.de' sitemap-generator-version='4.1.23' -->
<!-- generated-on='2026年4月27日 9:33 PM' -->
<sitemapindex xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd' xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>	<sitemap>
		<loc>https://law-text.com/sitemap-misc.xml</loc>
		<lastmod>2026-04-27T21:20:48+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://law-text.com/category-sitemap.xml</loc>
		<lastmod>2026-04-27T21:20:48+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://law-text.com/post-sitemap.xml</loc>
		<lastmod>2026-04-27T21:20:48+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://law-text.com/page-sitemap.xml</loc>
		<lastmod>2026-04-24T08:47:33+00:00</lastmod>
	</sitemap>
</sitemapindex><!-- Request ID: 5c8dd22e5244e8ae446e948cd2b30e62; Queries for sitemap: 7; Total queries: 60; Seconds: 0.01; Memory for sitemap: 1KB; Total memory: 8MB -->
