Support

Account

Home Forums General Issues Dynamically link 2 CPT through a common taxonomy

Solving

Dynamically link 2 CPT through a common taxonomy

  • Hi

    Is it possible to dynamically connect 2 CPT sharing the same taxonomy without setting the relationship on the Back-end?

    Example
    CPT1 = “Projects”
    CPT2 = “People”
    “city” is a shared taxonomy for both CPT

    When I show the page of a “Project” that is based in “Tokyo” (taxonomy “city”=”Tokyo”) I want to dynamically show on that page a list of “People” that are available to work in “Tokyo” (taxonomy “city”=”Tokyo”)

    I know I can do this setting up the relationship manually on the backend (one by one) but is there any way of doing this dynamically?

  • The depends on how the taxonomy is created and how it is associated with the CPTs.

    In your code to create the taxonomy do you set the post types that it is associated with?

    Does the taxonomy appear in the admin menu below both CPTs?

    Tell me more about the fields you use to set the terms for each post.

  • Thanks John
    Yes, the common taxonomy (city) is attached to both CPTs (Project and People). It appears associated to both on the ACF pannel and on the admin menu of both CPTs.
    (ACF Pannel setup under “Attach to Post Type” option. All the other options set to default except Hierarchical which was setup to Yes)
    Pedro

  • So, while it may look like the same taxonomy, the terms added under Project and the terms added under People are not the same terms. They used to be in the past but this was changed in WP 4.2

    So, as an example of what you need to do. Let’s say that you are showing a single “Project” post. You need to get the “City” term(s) associated with the project and then do a WP_Query() to get the posts from “People” using a tax_query.

  • Got it! Thanks for you help John.
    P

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Dynamically link 2 CPT through a common taxonomy’ is closed to new replies.