spock/shadow-taxonomies
Composer 安装命令:
composer require spock/shadow-taxonomies
包简介
A Composer library for creating relationships between custom post types using shadow taxonomies in WordPress.
README 文档
README
A WordPress Composer library for creating relationships between custom post types using shadow taxonomies.
Introduction
One of the hardest things to do in WordPress is creating relationships between two different post types. Often this is accomplished by saving relationship data in post meta. However this leads to expensive meta queries, which are generally one of the poorest performing queries you can make in WordPress.
Metadata can also be a pain to keep synced. For example, when posts are deleted, what happens to the post meta saved on a separate post type?
Shadow Taxonomy solves this by using WordPress taxonomies as the relationship layer. Instead of meta queries, you get performant taxonomy queries and a built-in checkbox UI on the post edit screen for free.
What is a Shadow Taxonomy?
A shadow taxonomy is a custom WordPress taxonomy that automatically mirrors a specific post type. Anytime a post in that post type is created, updated, or deleted, the associated shadow taxonomy term is also created, updated, and deleted.
This library manages the entire lifecycle of the shadow terms, keeping your taxonomy in sync with its associated post type.
Installation
composer require spock/shadow-taxonomies
Requirements: PHP >= 7.2, WordPress
Usage
Step One: Create the Shadow Taxonomy
add_action( 'init', function() { register_taxonomy( 'services-tax', 'staff-cpt', array( 'label' => __( 'Services', 'text-domain' ), 'rewrite' => false, 'show_tagcloud' => false, 'hierarchical' => true, ) ); // We will make our connection here in the next step. });
Here we are creating a normal custom taxonomy. In this example we are creating a taxonomy to mirror a CPT called Services, so by convention the shadow taxonomy is named services-tax.
Because we want to link Services to another post type called Staff, this taxonomy is registered on the Staff CPT post edit screen.
The taxonomy is not made public so that nobody manually edits the terms. The library handles creating, updating, and deleting the shadow taxonomy terms to keep everything in sync.
Step Two: Create the Association
\Shadow_Taxonomy\Core\create_relationship( 'service-cpt', 'service-tax' );
This one line creates the shadow taxonomy link. The first argument is the custom post type slug, and the second is the shadow taxonomy slug. Place this immediately after the register_taxonomy call.
Combined Helper
Use register_shadow_taxonomy to register the taxonomy and establish the relationship in a single call:
\Shadow_Taxonomy\Core\register_shadow_taxonomy( [ 'movies' ], [ 'actor' ], '_actor', [ 'label' => 'Actors', 'rewrite' => false, 'show_tagcloud' => false, 'show_ui' => false, 'hierarchical' => false, 'show_in_menu' => false, 'meta_box_cb' => false, 'show_in_rest' => true, ] );
API
get_the_posts
\Shadow_Taxonomy\Core\get_the_posts( $post_id, $taxonomy, $cpt )
Fetch the associated posts for a given post ID. Returns an array of WP_Post objects or false if none are found.
$post_id(int) required - The ID of the post whose associations you want to find.$taxonomy(string) required - The shadow taxonomy slug.$cpt(string) required - The associated custom post type slug.
get_associated_term
\Shadow_Taxonomy\Core\get_associated_term( $post, $taxonomy )
Get the shadow term for a given post. Accepts a WP_Post object or post ID. Returns a WP_Term object or false.
get_associated_post
\Shadow_Taxonomy\Core\get_associated_post( $term, $post_type )
Get the shadow post for a given term. Returns a WP_Post object or false.
get_meta_key
\Shadow_Taxonomy\Core\get_meta_key( $taxonomy, $type )
Build the meta key used to store shadow relationships. $type is either 'term_id' or 'post_id'.
Hooks
The library fires the following actions:
shadow_taxonomy_term_created- Fires after a shadow term is created. Parameters:$new_term,$post_id,$taxonomy.shadow_taxonomy_term_updated- Fires after a shadow term is updated. Parameters:$term,$associated_post,$taxonomy.shadow_taxonomy_term_deleted- Fires after a shadow term is deleted. Parameters:$term,$post_id,$taxonomy.
WP-CLI Commands
The library includes WP-CLI commands for managing shadow taxonomies on existing sites with existing data.
sync
wp shadow sync --cpt=<post_type> --tax=<taxonomy> [--dry-run] [--verbose]
Syncs all posts in the given post type to shadow terms, and removes any orphan terms.
sync-terms
wp shadow sync-terms --cpt=<post_type> --tax=<taxonomy> [--dry-run] [--verbose]
Like sync, but also repairs missing metadata on both the post and term side.
deep-sync
wp shadow deep-sync --cpt=<post_type> --tax=<taxonomy> [--dry-run] [--verbose]
Creates shadow terms for posts missing both the shadow meta key and a matching term by slug.
check
wp shadow check <post_type|taxonomy> --id=<int> --tax=<taxonomy>
Checks if a specific post or term has a valid shadow association.
Options
--cpt(string) required - The post type to shadow.--tax(string) required - The taxonomy to use as the shadow.--dry-run(flag) optional - Lists changes without making them.--verbose(flag) optional - Outputs additional logging during processing.
License
GPL-2.0+
spock/shadow-taxonomies 适用场景与选型建议
spock/shadow-taxonomies 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.92k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「taxonomy」 「Relationships」 「custom-post-type」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spock/shadow-taxonomies 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spock/shadow-taxonomies 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spock/shadow-taxonomies 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Interface for Babel Street Text Analytics
Adds nested resources routing support along with related actions and relationship handlers to the Yii RESTful API framework
Adds KeyBy('id') to Eloquent relationships
Icon support for Silverstripe taxonomy terms
Elemental Taxonomy module, provides a list of taxonomy terms under a selected type
WordPress plugin to automatically toggle the parent term when a sub term is selected.
统计信息
- 总下载量: 50.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2021-10-13