Home Forums General Issues Getting (and grouping) relation post object category


Getting (and grouping) relation post object category

  • Im in the following situation, i have custom post type called Materials which has a custom taxonomy material_category

    Then i have a custom post type called Doors with a custom field “material” that relate to one or more materials (specified above)

    When i create the single door page i have no problem to get all the related materials and print them out, also at the moment for each materia i get back i loop to get their category, then i store everything in a temporary object, order it by material_category and print it out to obtain somehhing like:

    Wood: Walnut, Pine
    Marble: White
    Lacquered: Matte, Gloss

    I found this way quiete intensive (the door have multiple custom field with relation to other objects that need the same category getting and grouping action)

    so i was looking for a way to (at least) get the post object with its categories so that i dont need to re-query each result.

    probably i can filter on ‘acf/fields/post_object/result’ but i cant find how
    any suggestion?

  • Without querying the DB directly there really is not any way to group post by its terms.

    You would need to
    1) Get the Post IDs in the relationship field.
    2) Get a list of all of the terms
    3) Do a query with a tax_query for each term and use the post IDs returned in #1 in the “post__in” argument

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

You must be logged in to reply to this topic.