0
votes

I've got multiple custom post types. Recipes and Ingredients. Recipe page contains ACF Relationship field to select ingredients. Ingredients custom posts belong to specific categories "Vegetables", "Meat"... On Ingredients Archive pages I would like to list recipes that consist of ingredients from the current category. Any help or directions would be appreciated.

$args = array(
'post_type' => 'recipes',
'meta_query' => array(
    array(
        'key' => 'ingredient_name', // custom field name
        'value' => '"' . get_terms('ingredients_categories') . '"', // assuming that this is getting post's categories
        'compare' => 'LIKE'
    )
)

);

1

1 Answers

0
votes

I had the same situation. I resolved this problem like this

for example you have custom field ingredients_field. This field is Multiple Select and in database it looks like this:

meta_key = ingredients_field_0 meta_value = 'some id of ingredient' meta_key = ingredients_field_1 meta_value = 'some another id of ingredient' ....

so, you need to find this key. for example you can add like this:

$current_ingredient_id = get_queried_object()->term_id;

for($counter = 10; $counter < 10; $counter++ ){
    $meta_query[] = array(
        'key'     => 'ingredients_field_' . $current_ingredient_id,
        'value'   => $item,
        'compare' => 'LIKE',
    );
}

$args = array(
'post_type' => 'recipes',
'meta_query' => $meta_query,
)
$the_query = new WP_Query( $args );

PS: I know this isn't perfect solution but it worked for me.

PPS: I'm not sure that keys in db builds like I wrote for you. Try to find in wp_postmeta table keys like yours