---
# System prepended metadata

title: 201027 - Reuse Categories in another blog | Only Posts in Home
tags: [KISD, Kunden, TH]

---

# 201027 - Reuse Categories in another blog | Only Posts in Home
We have categories (terms | taxonomy: affiliation) stored in root (blog_id: 1). We want to apply those categories to to posts in home.
We also want regular categories in home (announcement, news, question).

## There is many ways to go about this

### move home to root (home = root) (preferred?)

* 👍 simple
* 👍 overlapping pages (with blogs) is not a problem.
* 👍🎉 You can create pages like before. A page like spcs.com/page would become spcs.com/page.
* 👎 it would be nice to seperate home & root to different shards (thinking about load)
http://spaces.test/start
- ✅ idea: prefix posts with /home/
  - ✅ create post "abc" -> can create space "abc"
  - ✅ create page "abc" -> can't create space "abc"
  - ✅ create a post "ttt" -> create a page "ttt" -> page will be automatically named "ttt1"
  - ✅ 😐  ❓ could be better, but okay. create space "def" -> create page "def".  page is only accessible with prefix
  - ✅ 😐 (only with plain permalink setting) admin create page -> user adds post with the same name -> post is not accessible (page overwrites)
  - ✅ Make sure, that the proper (date) permalink structure is selected for posts! Add an [admin_notice](https://developer.wordpress.org/reference/hooks/admin_notices/).
      - ~~ML: Done~~
      - VS: Hm, i guess this was not really needed?

* ✅👷‍♂️ if you change the siteurl of root, you can still create a blog with the same name: prevent this! - Done for frontend!
* ✅ 👷‍♂️ spaces-core currently checks for the existence of a blog, before it can be used as home [here](https://github.com/dol-lab/spaces/blob/master/web/app/plugins/spaces-core/includes/classes/SettingsSettings_Network_General.php#L248). Think about a solution. 
    * ~~ML: Sol. 1. Check not only for the Slug of a Blog, also use the blog id.~~
    * ~~ML: Sol. 2. Allowed the Home_URL from rootblog as entry, get_home_url(0)-> spaces.test/home and take home->~~ 
    * ✅ Just allow root as a blog via "/"
    * ~~ML: Sol. 3. Allowed as default empty which is the home_url of the root blog.~~
    * ~~ML: Sol. 4. Allow / for setting. Results in redirection error ->fixed~~
* ✅👷‍♂️ (is fixed, when we adjust ``wp_options:option_name:home != ''``) things currently work in twenty-twenty-theme. our defaultspace needs some fixes
* 🏃‍♀️ 👷‍♀️ user is not properly redirected after login.
* 🏃‍♀️ 👷‍♀️ don't allow users to create pages. 
    * ML: Can be done with the Role Editor Plug-In (very easy) or (Done) 
    * ~~ML: Programatically (easy) by removing the page capabilities by activating the Theme and only for root blog.~~


### hack wp_query (preferred?)

- ❓ Whenever we query add a post_affiliation query with relation 'AND'
- ❓ Whenever we store a post, also store the post_affiliation (but where?)
### just switch the prefix of (preferred?) ``$wpdb->terms`` & ``$wpdb->term_taxonomy``
- Exampe: home blog id: 4
  - $wpdb->terms = 'wp_4_terms' => 'wp(_1)_terms'
  - $wpdb->term_taxonomy = 'wp_4_term_taxonomy' => 'wp(_1)_term_taxonomy'
  - $wpdb->term_taxonomy = 'wp_4_term_taxonomy' => 'wp(_1)_term_taxonomy'
  - https://github.com/sbrajesh/multisite-global-terms/blob/master/mu-global-terms.php
- 👍 Relathinships are stored in home
- 👍 No issues with overlapping taxo-ids.
- 👎 terms & taxonomies have to be created in root
- 👎🤯 can table joins be done across different hosts (databases) https://stackoverflow.com/questions/8752477/joining-tables-from-different-servers
### just store all the things in home
- 👎 Not intuitive/clean
- 👎 Not optimal with sharding
- 👍 "Easy" to implement, no cross-blog categories needed.

### sync terms (have them both in root & home)
- 👎 ❓ how do we deal with overlapping term-ids? 💡 sync via slug?
- 👎 you need check in root: when a taxonomy changes, it also needs to change in home : some of code necessary
- 👎 if the term slug already exists in the home blog (low probability) it can't exist again with the same slug (https://developer.wordpress.org/reference/functions/wp_unique_term_slug/). So maybe sync via term name?
- ❓ what do we use for 
- 
### specify a custom ``$object_type`` in `WP_Taxonomy` Class which references another blog  
  'post' 'post_in_blog_1'
 - 👎 saved in root
### [Multisite-Taxonomies](https://github.com/HarvardChanSchool/multisite-taxonomies)
-  👎 We also store things in a central place.
-  👎 introduce another system


⚡ Make sure the WP-Query for posts work as usual
```php
$the_query = new WP_Query( array(
    'tax_query' => array(
        'relation' => 'AND',
        array (
            'taxonomy' => 'post_affiliation',
            'field' => 'slug',
            'terms' => 'f01',
        ) 
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'announcement',
        ),
    ),
) );

```

## Case #store-relation-in-root
The term_taxonomy_id


### DB - Structure

## `wp_term_relationships`
Brings together terms & objects (posts).


| object_id           | term_taxonomy_id                 | term_order |
| ------------------- | -------------------------------- | ---------- |
| post_id/user_id/... | unique( term_id & taxonomy_name) | ---        |
| 2 (user)            | 1                                | ---        |
| 22 (blog)           | 3                                | ---        |
| 23 (blog)           | 4                                | ---        |
| 24 (blog)           | 3                                | ---        |
| 25 (blog)           | 3                                | ---        |


## `wp_terms` | wp_1_terms | wp_4_terms (home)

| term_id | name         | slug | term_group (obsolete) |
| ------- | ------------ | ---- | --------------------- |
| 1       | f01          | -    | -                     |
| 2       | f02          | -    | -                     |
| 3       | announcement | -    | -                     |

## `wp_term_taxanomy`

Junction table (between a PHP taxonomy and wp_terms). Assigns a taxonomy (with a name) to a term.

| term_taxonomy_id | term_id | taxonomy         | description | parent | count |
| ---------------- | ------- | ---------------- | ----------- | ------ | ----- |
| 1                | 1       | post-affiliation | 0           | 0      |       |
| 2                | 1       | user-affiliation |             |        |       |
| 3                | 1       | blog-affiliation |             |        |       |
| 4                | 2       | blog-affiliation |             |        |       |
| 5                | 3       | category         |             |        |       |

## `wp_terms`

| meta_id | term_id | meta_key | meta_value |
| ------- | ------- | -------- | ---------- |
|         |         | -        | -          |




###### tags: `KISD` `TH` `Kunden`