最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

My get_terms not working for custom fields

programmeradmin1浏览0评论

This works and generates results:

{% set query_parameters = {
   taxonomy: term.taxonomy,
   parent: term.id,
} %}
{% set terms = wordpress.call('Timber::get_terms', query_parameters) %}

But this doesn't:

{% set query_parameters = {
   taxonomy: term.taxonomy,
   parent: term.id,
   meta_query: {
     0: {
       key: 'test_type',
       value: '8114',
       compare: 'LIKE',
     },
   },
} %}
{% set terms = wordpress.call('Timber::get_terms', query_parameters) %}

test_type is a custom field created with ACF. A dump of the results that do work contains:

["test_type"]=> array(2) { [0]=> int(8114) [1]=> int(8115) }

What could be wrong?

This works and generates results:

{% set query_parameters = {
   taxonomy: term.taxonomy,
   parent: term.id,
} %}
{% set terms = wordpress.call('Timber::get_terms', query_parameters) %}

But this doesn't:

{% set query_parameters = {
   taxonomy: term.taxonomy,
   parent: term.id,
   meta_query: {
     0: {
       key: 'test_type',
       value: '8114',
       compare: 'LIKE',
     },
   },
} %}
{% set terms = wordpress.call('Timber::get_terms', query_parameters) %}

test_type is a custom field created with ACF. A dump of the results that do work contains:

["test_type"]=> array(2) { [0]=> int(8114) [1]=> int(8115) }

What could be wrong?

Share Improve this question edited Dec 1, 2016 at 2:57 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 30, 2016 at 16:05 RubyTuesdayRubyTuesday 215 bronze badges 5
  • What language is this? - set query_parameters = {..etc – Dan. Commented Nov 30, 2016 at 16:15
  • Confused by the syntax here as well, but meta_query requires an array of arrays. Regardless of language, it looks like you're putting it as an sub-object in the object property 0. – Nic Commented Nov 30, 2016 at 16:23
  • It's being processed through TWIG using Timber. I've used this syntax elsewhere where it's worked. The only fundamental difference I can see is that here query_parameters is being used with get_terms (and not get_posts where I've used it previously). – RubyTuesday Commented Nov 30, 2016 at 17:01
  • Looks like I may have found my answer: support.advancedcustomfields/forums/topic/… and support.advancedcustomfields/forums/topic/… - going to test it out and will report back. – RubyTuesday Commented Nov 30, 2016 at 17:25
  • I would try to replicate this with PHP for starters. Given a lot of third party code involved who knows at which point does it go wrong. – Rarst Commented Nov 30, 2016 at 17:40
Add a comment  | 

1 Answer 1

Reset to default 1

The issue is with ACF as discussed here along with a solution: https://support.advancedcustomfields/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/

Courtesy of various posters there, the code I added to mu-plugins is:

function acf_update_term_meta($value, $post_id, $field) {
  $object = get_queried_object();
  $term_id = intval(filter_var($post_id, FILTER_SANITIZE_NUMBER_INT));
  if (!isset($object->taxonomy) || !isset($object->term_id) || $term_id != $object->term_id) {
    // the current object is not a term
    return $value;
  }
  update_term_meta($term_id, $field['name'], $value);
  return $value;
}

add_filter('acf/update_value', 'acf_update_term_meta', 10, 3);

function acf_load_term_meta($value, $post_id, $field) {
    $term_id = intval(filter_var($post_id, FILTER_SANITIZE_NUMBER_INT));
    if($term_id > 0)
        $new_value = get_term_meta($term_id, $field['name'], true);
    return $new_value ? $new_value : $value;
 }
 add_filter('acf/load_value', 'acf_load_term_meta', 10, 3);

One difference is that when loading term meta I check that a value is actually returned from get_term_meta and if not I return the original value. This serves to not loose existing meta values, and so that they can then be updated in the new way.

Note that these values cannot be queried until they have been updated in the new way, and this requires editing and saving all terms.

The final thing to note is that if the meta value is an object it appears that it is only the id can be queried.

发布评论

评论列表(0)

  1. 暂无评论