如何使用带有分类术语的自定义字段的ACF获得真实term_meta(而不是wp_options) [英] How to get real term_meta using ACF with custom fields on taxonomy terms (instead of wp_options)

查看:177
本文介绍了如何使用带有分类术语的自定义字段的ACF获得真实term_meta(而不是wp_options)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Woocommerce网站,我正在使用ACF pro在给定分类法的条款中添加一些自定义字段。例如,使用此功能,我可以在产品类别分类术语中添加颜色字段。好。

Having a simple Woocommerce site, I'm using ACF pro to add some custom fields to the terms of a given taxonomy. Using this, I can, for example, add a "color" field to my "product category" taxonomy terms. Good.

问题:

想要使用<$ c $进行查询c> get_terms()( codex ),我发现我的 meta_query 参数不起作用(意外结果)。为什么呢我的自定义字段没有(从后端)保存为 term_meta ,而是保存为 wp_option

Wanting to do some query using get_terms() (codex), I discovered that my meta_query parameters were not working (unexpected results). Why ? My custom fields were not saved (from backend) as term_meta but as wp_option.

似乎ACF 4正在保存这些字段,而不是保存为 term_meta (按设计目的),而是保存为 wp_option ,在Wordpress选项表中。因此,您不能使用 get_terms()来按 terms_meta 查询项( meta_query codex )来获得基于某些 term_meta 值。

It seems that ACF 4 is saving those fields, not as term_meta (as it's designed for), but as wp_option, in the Wordpress options table. So you cannot "query terms by terms_meta" (meta_query) using get_terms() (codex) to get some terms based on some term_meta value.

推荐答案

我可以通过以下操作来解决此问题:

I could fix this doing the following:

比方说我在分类学术语中有2个自定义字段: color shape (意味着我在给定分类法的后端术语编辑/创建页面上输入了颜色和形状)。

Let's say I have 2 custom fields on my taxonomy terms: color and shape (means I have a color and shape input on my backend terms edit/create page for the given taxonomy).

function acf_update_term_meta( $value, $post_id, $field ) {
    $term_id = (int) filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT );
    if ( $term_id > 0 ) {
        update_term_meta( $term_id, $field['name'], $value );
    }

    return $value;
}
add_filter( 'acf/update_value/name=color', 'acf_update_term_meta', 10, 3 );
add_filter( 'acf/update_value/name=shape', 'acf_update_term_meta', 10, 3 );

function acf_load_term_meta( $value, $post_id, $field ) {
    $term_id = (int) filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT );
    if ( $term_id > 0 ) {
        $value = get_term_meta( $term_id, $field['name'], true );
    }

    return $value;
}
add_filter( 'acf/load_value/name=color', 'acf_load_term_meta', 10, 3 );
add_filter( 'acf/load_value/name=shape', 'acf_load_term_meta', 10, 3 );

所以我们有:


  • 使用 update_term_meta()来更新 term_meta 的过滤器( codex )在更新此ACF字段时(被选中两次,一次为 color 和一个用于 shape

  • 过滤器,返回 term_meta 使用 get_term_meta() codex ),而不是 wp_option (钩了2次,一次为 color ,另一次为 shape

  • a filter to update the term_meta using update_term_meta() (codex) when updating this ACF fields (hooked 2 times, one for color and one for shape)
  • a filter to return the term_meta value using get_term_meta() (codex) instead of the wp_option (hooked 2 times, one for color and one for shape)

来源

注释1

Note 1:

这将触发所有字词(无哑光)和分类法)具有颜色 shape 自定义字段的字段。如果您不希望它始终适用于这些字段,则可能需要按分类法进行过滤。

this will trigger for ALL terms (no matter the taxonomy) having the color or shape custom field. You may need to filter by taxonomy if you don't want it to always apply in those fields cases.

注释2

Note 2:

ACF5 似乎支持开箱即用的真实 term_meta ,但仅处于早期使用状态。
升级过程似乎包含针对此特定情况的重构方法(将数据从 wp_options 复制到实际的 term_metas ):

ACF5 seems to support out-of-the-box real term_meta, but is still in early access only. The upgrade process seems to contain a refactor method for this particular case (duplicate data from wp_options to real term_metas):


更新到ACF 5后,将提示您升级数据库。

After updating to ACF 5, you will be prompted to upgrade the Database.

这是从版本4.x跨字段和字段组
设置迁移的必要步骤。此升级还将跨
个分类术语值从'wp_options'表复制到'wp_termmeta'
表。

This is a necessary step to migrate across your field and field group settings from version 4.x. This upgrade will also copy across any taxonomy term values from the 'wp_options' table to the 'wp_termmeta' table.

不删除任何数据或在升级期间进行了修改。 (

No data is deleted or modified during this upgrade. (source)

注释3 :我相信可以在所有ACF术语自定义字段上循环以生成此代码自动对术语中的所有自定义字段进行操作,并避免为每个新的ACF术语字段添加2个过滤器。但是,由于ACF5应该很快就会淘汰,所以这可能不值得。

Note 3: I believe it would be possible to loop on all ACF terms custom fields to "generate" this code automatically for all custom fields on terms, and prevent having to add 2 filters for each new ACF term field. But as ACF5 should go out soon, it may not be worth the time.

这篇关于如何使用带有分类术语的自定义字段的ACF获得真实term_meta(而不是wp_options)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆