Custom Fields
Re-Envisioned
Support MB Relationships Term to Term connections?

  • Creator
    Topic
  • #18843
    Resolved MattTaylor
    Participant

    How do I make teen to term connections, when both terms belong to different taxonomies? Nothing I try here works. :/ Thank you!!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #18844
    MattTaylor
    Participant

    … New Siri autocorrect is a real hassle. 🙂

    #18849
    Long Nguyen
    Moderator

    Hi Matt,

    You can create a relationship between terms easy with this code

    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::register( [
            'id'   => 'term_to_term',
            'from' => [
                'object_type' => 'term',
                'taxonomy'    => 'category',
            ],
            'to'   => [
                'object_type' => 'term',
                'taxonomy'    => 'product_cat',
            ],
        ] );
    } );

    an example between the category of post and product category of Product (WooCommerce). Then this code will show the terms which are related

    $args = array(
        'taxonomy' => 'product_cat',
        'relationship' => [
            'id'   => 'term_to_term',
            'from' => get_queried_object_id(), // You can pass object ID or full object
        ],
    );
    $terms = get_terms( $args );
    
    foreach ( $terms as $term ) {
        echo $term->name . '<br>';
    }

    To manipulate with the terms, please follow this documentation.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.