简体   繁体   English

Wordpress 自定义帖子中的 ACF 关系

[英]Wordpress ACF Relationships in custom posts

I've got multiple custom post types.我有多种自定义帖子类型。 Recipes and Ingredients.食谱和配料。 Recipe page contains ACF Relationship field to select ingredients.配方页面包含与 select 成分的 ACF 关系字段。 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'
    )
)

); );

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:该字段是 Multiple Select 并且在数据库中它看起来像这样:

meta_key = ingredients_field_0 meta_value = 'some id of ingredient' meta_key = ingredients_field_1 meta_value = 'some another id of ingredient'.... meta_key = ingredients_field_0 meta_value = '一些成分的 id' meta_key = ingredients_field_1 meta_value = '另一种成分的 id'....

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. PS:我知道这不是完美的解决方案,但它对我有用。

PPS: I'm not sure that keys in db builds like I wrote for you. PPS:我不确定 db 中的键是否像我为你写的那样构建。 Try to find in wp_postmeta table keys like yours尝试在 wp_postmeta 表中找到像你这样的键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM