0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

マルチサイト時にカスタムフィールド含めて、タグクラウドやカテゴリを共通にする

Last updated at Posted at 2023-02-25

以下を参考にカテゴリーの共通化はできましたが、
カテゴリーに紐づいているACFのカスタムフィールドは表示されないので、そこを共通化するように変更します。
https://www.tomiryu.com/wordpress/mu-central-taxonomie/

コード

add_action('init','central_taxonomies');
add_action('switch_blog','central_taxonomies');
function central_taxonomies () {
    global $wpdb;
    global $pagenow;
    $wpdb->terms = $wpdb->base_prefix."terms";
    $wpdb->term_taxonomy = $wpdb->base_prefix."term_taxonomy";
    //追加ここから
    if(is_admin() && ($pagenow === "edit-tags.php" || $pagenow === "term.php")){
        $wpdb->termmeta = $wpdb->base_prefix."termmeta";
        $wpdb->posts = $wpdb->base_prefix."posts";
        //acfはpostsにカスタムフィールドの設定が入るのでカテゴリー編集時のみ親サイトページを読むようにする
    }
    //追加ここまで
}

解説

termmetaだけそのまま読むようにしてもacfはpostsに各カスタムフィールドの設定が入るので表示されません。
postsを全体で読むようにすると、今度は固定ページなども親サイトの内容を読むようになるので、条件分岐してカテゴリー追加もしくは、編集時のみ読むようにして上げて完了です。

フロントでカスタムフィールドの内容を表示する場合は「switch_to_blog」で親サイトを読むようにします。

失敗例

条件分岐させずに直接編集時に読み込ませたほうが良いと思ったので、追加しましたが、おそらく読み込みの順番が遅いので効きませんでした。

add_action('load-edit-tags.php','edit_tags_page');
function edit_tags_page() {
    global $wpdb;
    $wpdb->termmeta = $wpdb->base_prefix."termmeta";
    $wpdb->posts = $wpdb->base_prefix."posts";
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?