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 |1 Answer
Reset to default 1The 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.
set query_parameters = {
..etc – Dan. Commented Nov 30, 2016 at 16:15meta_query
requires an array of arrays. Regardless of language, it looks like you're putting it as an sub-object in the object property0.
– Nic Commented Nov 30, 2016 at 16:23