I take that back. I could not access $_GET
. I can access $_REQUEST
(or $_POST
specifically).
I get this from $_REQUEST
:
Array
(
[paged] => 1
[taxonomy] =>
[post_type] =>
[s] =>
[max] =>
[min] =>
[action] => acf/fields/relationship/query
[field_key] => field_5a5deb8769362
[post_id] => term_7
[nonce] => 5aa22de3aa
)
So you can use ‘post_id’ and replace ‘term_’.
if ( ! empty($_REQUEST['post_id']) ) {
$args['tax_query'] = array(
array(
'terms' => str_replace( 'term_', '', $_REQUEST['post_id'] );
),
);
}
I think that you missed the important point of my article. Make sure you read the code example and the comments in the example.
You’re using update_field()
which is updating an acf field and the new field is outside of ACF and added using standard WP function https://codex.wordpress.org/Function_Reference/add_post_meta
You’re also updating the field with an array and you can’t do a query based on an array stored in the database.
The idea is that you get each row of the repeater. Concatenate something into a string, for example 100/90/17
and then save each of these strings using add_post_meta(). Then when you query the posts you do a meta query on this new meta_key and the value you query by is the concatenated a similarly concatenated string.
This will be more difficult in your case because you’re using a taxonomy for these values so you’ll need to get the string values you want to concatenate from the terms. But the idea is the same. You need to have unique values stored into a standard WP custom field that you can query on.
You would need to add a tax_query https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Your location rules should be
Taxonomy Term
is equal to
category
AND
Post Category
is not equal to
World News
Never mind.
I think it was because I hadn’t included the ‘location’ taxonomy in the ‘taxonomy’ argument of register_post_type. Adding that in, but setting 'meta_box_cb' => false
, means that WP is correctly counting these items, but not showing a default WP metabox on the admin screen.
Found the cause of this issue by print_r’ing the entire query and inspecting what was actually going on – had done this a number of times, but overlooked the following detail:
The issue here wasn’t actually due to the checkbox meta_query, but a conflict between the get_query_var term (‘courses’) and a custom taxonomy whose slug is also ‘courses’.
When the query was executed, the query seemed to be querying for posts associated with both the custom taxonomy and the selected checkbox value, yielding no results.
Renaming the name attr to something other than courses then reconfiguring the registered query var and meta_query fixed this issue.
Hi John and @koiastudio,
Eliot has fixed the issue now. You need to reinstall a new version of 5.6.7 and it should be fine. Regarding the WP Taxonomy list page, I agree with you John it’s a bit weird.
I think it has something to do with the way the form on the category list page reloads through AJAX. I think this is a new feature on this page in WP. To be honest, I dislike the fields appearing here and I wish that WP would make the add work like the posts, opening the term edit page instead of having it directly on the list page, but that would only be useful for us people that highly customize the taxonomy/terms editor.
So I found at least a solution to get the fields from the “first” taxonomy.
It’d be great if someone could tell me how to randomize this!
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'movies', array('fields' => 'ids'));
//print_r($terms);
echo $terms[0];
?>
<?php echo get_field('director', 'movies_'.$terms[0]); ?>
This is a copy of my example here https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-post-category-ancestor-location-rule.php with 1 line difference
This difference is marked, look for
// this line added to include this term
// **********************************************************************************
$ancestors[] = $term_to_check->term_id;
// **********************************************************************************
You may want to change other things, like the value of the location rule, maybe the function names.
I have not tested this, but if my example is still working then this change should make it include any category and any children of that category
<?php
// category ancestor location rule
add_filter('acf/location/rule_types', 'acf_location_types_category_ancestor');
function acf_location_types_category_ancestor($choices) {
if (!isset($choices['Post']['post_category_ancestor'])) {
$choices['Post']['post_category_ancestor'] = 'Post Category Ancestor';
}
return $choices;
}
add_filter('acf/location/rule_values/post_category_ancestor', 'acf_location_rule_values_category_ancestor');
function acf_location_rule_values_category_ancestor($choices) {
// copied from acf rules values for post_category
$terms = acf_get_taxonomy_terms('category');
if (!empty($terms)) {
$choices = array_pop($terms);
}
return $choices;
}
add_filter('acf/location/rule_match/post_category_ancestor', 'acf_location_rule_match_category_ancestor', 10, 3);
function acf_location_rule_match_category_ancestor($match, $rule, $options) {
// most of this copied directly from acf post category rule
$terms = 0;
if (array_key_exists('post_taxonomy', $options)){
$terms = $options['post_taxonomy'];
}
$data = acf_decode_taxonomy_term($rule['value']);
$term = get_term_by('slug', $data['term'], $data['taxonomy']);
if (!$term && is_numeric($data['term'])) {
$term = get_term_by('id', $data['term'], $data['taxonomy']);
}
// this is where it's different than ACf
// get terms so we can look at the parents
if (is_array($terms)) {
foreach ($terms as $index => $term_id) {
$terms[$index] = get_term_by('id', intval($term_id), $term->taxonomy);
}
}
if (!is_array($terms) && $options['post_id']) {
$terms = wp_get_post_terms(intval($options['post_id']), $term->taxonomy);
}
if (!is_array($terms)) {
$terms = array($terms);
}
$terms = array_filter($terms);
$match = false;
// collect a list of ancestors
$ancestors = array();
if (count($terms)) {
foreach ($terms as $term_to_check) {
// this line added to include this term
// **********************************************************************************
$ancestors[] = $term_to_check->term_id;
// **********************************************************************************
$ancestors = array_merge(get_ancestors($term_to_check->term_id, $term->taxonomy));
} // end foreach terms
} // end if
// see if the rule matches any term ancetor
if ($term && in_array($term->term_id, $ancestors)) {
$match = true;
}
if ($rule['operator'] == '!=') {
// reverse the result
$match = !$match;
}
return $match;
}
?>
I was having exactly the same problem. Things were working well until I introduced some filters to include in the search results matches by taxonomy terms. The filters I was using came from a 3rd party site. The code was the one below. On its own it works well and was helping with my search results (so if someone enters a tag name it will also return associated posts). But there’s something that caused conflicts with ACFPro.
function atom_search_where($where){
global $wpdb;
if (is_search())
$where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
return $where;
}
function atom_search_join($join){
global $wpdb;
if (is_search())
$join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
return $join;
}
function atom_search_groupby($groupby){
global $wpdb;
// we need to group on post ID
$groupby_id = "{$wpdb->posts}.ID";
if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;
// groupby was empty, use ours
if(!strlen(trim($groupby))) return $groupby_id;
// wasn't empty, append ours
return $groupby.", ".$groupby_id;
}
add_filter('posts_where','atom_search_where');
add_filter('posts_join', 'atom_search_join');
add_filter('posts_groupby', 'atom_search_groupby');
This means that get_term_by()
is probably returning false. This means that either the taxonomy or the term does not exist.
Would you mind showing me how this last code would look like if the output of that image field HAD to be the image ID?
I´m trying to get it to work on a CPT’s single.php and show the custom taxonomy term’s image this post is associated with.
Any hint would be appreciated!
This is just a guess, but it’s what I think is going on. Since the select2 field is loaded using AJAX then somehow WP is blocking the ACF from getting the list of terms from the taxonomy if the user is not logged in.
At any rate, something is preventing the AJAX request from completing.
That being said, I just tried a taxonomy field, on a font end form using acf_form(), while logged out and it works perfectly. This is using current version of WP, 2017 them, ACF Pro and no other plugins and testing using the built in “category” taxonomy.
I too have this problem.
I have a custom taxonomy field that looks like this:
register_taxonomy('product_category', 'product', array(
'hierarchical' => true,
'label' => 'Product Category',
'meta_box_cb' => false, // Use ACF to display
'show_in_quick_edit' => false,
//'rewrite' => array( 'slug' => 'cat', 'with_front'=>false ),
));
However when I use this within a Select field, no options are provided?
I have had to resort to keeping this a Radiobox for now, however I have Sub Categories as well, so the form is getting quite large.
Any help would be MUCH appreciated.
Hello
I’m trying to use the acf/fields/taxonomy/result/ filter, but without any result.
Even if I enter a simple test code like this:
function tp_taxonomy_result( $title, $post, $field, $post_id ){
$title = 'test text';
return $title;
}
add_filter('acf/fields/taxonomy/result/key=field_5a046d4c673af', 'tp_taxonomy_result', 10, 4);
I do not get any results in my taxonomy box.
Any idea why the filter does not seem to work?
Hi Elliot,
that’s so weird – now I also can’t reproduce the issue anymore. Yesterday, I switched my modules layouts from table
to row
, because in the table layout mode the conditional logic wasn’t working at all anymore. At least that I can still reproduce today. How to reproduce:
– switch the ‘Category List’ Module to table
-layout
– now the taxonomy select doesn’t have any effect anymore, it’s always showing the category tax field
I figured this out. I totally misused the repeater field which caused an unnecessary headache.
Anyway, this is the code I used. It’s a modified version of this code – https://support.advancedcustomfields.com/forums/topic/query-custom-taxonomy-name-and-link-inside-repeater/
Hope this helps someone out.
<?php
// loop through the repeater
if (have_rows('catimg_repeater')) {
while (have_rows('catimg_repeater')) {
the_row();
$term = get_sub_field('choose_category');
$term_link = get_term_link($term);
$image = get_sub_field('choose_image');
$url = $image['url'];
$alt = $image['alt'];
?>
<a href="<?php echo $term_link; ?>"><img src="<?php echo $url ?>" alt="<?php echo $alt ?>" /></a>
<?php
} // end while have rows
}
?>
I have a custom field with location rule on custom post type and i want to check this custom field value(Radio button field) on Taxonomy terms page .How can do that
is premium ? Yes No
this is a field on custom posts ‘listing’ and i want to check this field value on
‘taxonomy-listing’ Page .
I don’t want them to use inside WP Query , But want to run various WP Query according to value of ‘is premium?’
Pls Help
It depends on the query. All queries based base on meta_value are going to be slower. The reason for this is that the meta_value field in the DB is not an indexed field.
The more meta values you’re searching on, the slower your query will be.
Some types of queries are even most costly, for example “LIKE” queries on the meta_value is going to be slower than ‘=’
There really isn’t a way to “optimize” these queries any more than what is being done. The only way to improve these is to
1) Write your own SQL to get what you need and query the db directly instead of depending on WP
2) Pre index the search results for each possible search and store these results for use instead of doing the query.
On the other hand…
If you’re talking about the queries that ACF runs when you call get_field() or one of the other functions for getting values, it depends on the type of field. There are some fields that will be slower than others.
For example, an image field, if you have ACF set to return and array or image object, this will be slow because ACF has to do a lot of queries to build that image array. When it comes to image fields you are better off only returning the attachment ID and then using WP functions to only get what you need. The same is true of any of the fields that can return WP objects like taxonomy fields, post object fields, relationship fields. For performance you will always be better off getting the ID and then using it to call WP functions yourself. The reason is that ACF is probably getting a lot more information than you really need and doing extra queries to get this unneeded information.
In addition to this, if you are getting values for a post that is not the current post in the loop… example:
if (have_posts()) {
while (have_posts()) {
the_post();
// now you get several values for another post
// that is not the global $post in this loop
}
}
This will add additional queries, before getting any values using get_field(), get_sub_field(), etc you might see a reduction in the number of queries performed by calling
get_post_meta($other_post_id);
What this does is to get all of the meta values for the other post in a single query rather than doing a query for each field. This may or may not give you an improvement, I have yet to figure out under what conditions WP does this automatically and when it does not, but if WP has already loaded all of the meta values for a post, calling the function in the manner will not create any additional queries, so it can’t really hurt.
I believe that ACF sets other values in $_POST for the taxonomy and the term id, but I am not sure. It should allow you to see if it’s a term page then alter the query to check terms instead of posts.
To see whats in $_POST during the ajax request you can do something like this
ob_start(); print_r($_POST); error_log(ob_get_clean());
This will print out the value of $_POST to your error log so you can inspect what’s in it.
Yeah, I think a taxonomy for related_stories gets in to management difficulty, but definitely pressing ahead with one for publication sources.
Thanks.
Hello John,
I need your help.
I am creating one text field “place_id” on “property” taxonomy.
How to check duplicate entry when submitting post, and set validation ??
Thanks
This is actually a good idea. I have also wished on sites with many custom field groups for a way to better manage them.
ACF uses the standard post admin pages. This means that you can alter the list of posts to include other columns, for example a category, just like you can alter this list for any other post type.
I don’t see why you could not add a custom taxonomy to the acf-field-group
post type. This should add a standard tags or category box to the right side of the page to select a category.
The only thing that I’m not sure about would be sorting and filtering the list of groups by this new group category and you’d need to look into that.
I don’t think any of this is impossible.
I’m not the developer, so I can’t answer the other questions. You might be able to get an answer from the devs by opening a new support ticket here https://support.advancedcustomfields.com/new-ticket/
Not realy an ACF question. Aber vielleicht:
$terms = array('1-herren');
$abfrage = array(
'post_type' => 'spieler',
'tax_query' => array(
array(
'taxonomy' => 'manschaft',
'field' => 'slug',
'terms' => $terms,
),
),
);
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.