Support

Account

Home Forums ACF PRO Call for posts on archive page if their relationship item is matching category

Solved

Call for posts on archive page if their relationship item is matching category

  • Hi guys,

    I’m trying to understand the right logic and it would be great if you could point me in the right direction, please. Share examples.

    I’ve got a few custom post types: Recipes and Ingredients.
    Recipe posts contain ACF Relationship post selector from Ingredients.

    What I would like to achieve is to call for recipes on the Ingredients taxonomy page if one of the listed ingredients in a recipe post has a matching taxonomy category.

    As an example: Vegetables archive page lists recipes that consist of Cauliflower(custom post page with category = Vegetables) as an ingredient.

    In the documentation, I can see something similar here. How can I check if the recipe post-relationship ingredient category is matching with the taxonomy page..?

  • if I understand the relationships:

    CTP Recipes -> Relationship -> Ingredients
    Ingredient -> Taxomomy -> Ingredient Type (ie vegetables)

    on “vegetables” page you would need to do a query to get every ingredient post with the term (already done by WP, but you’d need to do it yourself to get the list of post IDs). Then you would need to use all of the posts returned from this query to get the recipe posts that have these ingredients.

    Unfortunately, the query to get all of the recipes will likely grow out of hand and will very likely cause a time out on the DB query. Let’s say that you have 20 ingredients listed as veggies, then you’d be doing 20 “Like” queries in an ‘OR’ meta query which would be extremely bad.

    My suggestion would be to set up a bidirectional relationship field (see https://www.advancedcustomfields.com/resources/bidirectional-relationships/ OR https://wordpress.org/plugins/post-2-post-for-acf/) between recipes and ingredients. With a bidirectional field you would not need to do any additional queries. You would simply loop over the ingredients and then get the recipe relationship.

  • Hi John, that makes sense! Unfortunately, for myself, ingredients relationship is a subfield and listed in both repeater and flexible content. I’ve attached design just to illustrate how it’s done on a recipe page. Thanks for pointing me the bidirectional way, have you seen anyone achieving similar having relationship as a subfield?

  • No, I have not seen anyone have success with making a bidirectional relationship field in a repeater work.

    In this case I would skip the bidirectional relationship and I would create a field for ingredients use only for making it easier to search. This field would just be a standard WP custom field with no need to be an ACF field that can be edited. It would not be editable from the ingredient side, only the recipe side. This would be a standard WP meta field that could hold multiple values.

    What I would do is I would create an acf/save_post action for recipes priority <10 so that it runs before ACF. I would loop through the repeater twice, once on the old values and once on the new values to collect a list of both old and new. Then compare and add/remove/update the meta key on for each ingredient.

    It would work very similar to a bidirectional field but instead getting the field to get the recipes on the ingredient side you would get the meta value and then use that value to query the recipes for each ingredient. Still not optimal, but better than a string of like queries.

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

The topic ‘Call for posts on archive page if their relationship item is matching category’ is closed to new replies.