The first step with creating a front end form to allow users to edit a term would be supplying the correct post ID when calling acf_form().
For an existing term the post ID you need will be "term_{$term_id}"
For allowing them to create a new term you would need to build a pre_save_post filter that creates the term and sets the correct post ID to be returned to ACF so that it can to the updating https://www.advancedcustomfields.com/resources/acf-pre_save_post/. In this case you would use something like "new_{$taxonomy)_term"
for the post ID and use this in your conditional at the beginning of your filter.
<?php
$scat = get_the_term_list( $id_product,”scat”); // I add scat taxonomy for each product to manually publish the Category ID of the blog
$args = array(
‘post_type’ => ‘post’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category’,
‘terms’ => array( $scat ),
),
);
// query
$query = new WP_Query( array( ‘cat’ => $args ) );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
$content = apply_filters(‘the_content’, $post->post_content);
<?php
if( have_rows(‘chapter’) ) {
while( have_rows(‘chapter’) ) {
the_row();
$row = array(
‘link’ => $content ,
);
add_sub_row(‘link’, $row);
}
}
?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Latest update.. It’s still not working ;(
more update*
<?php
$scat = get_the_term_list( $id_product,”scat”);
$args = array(
‘post_type’ => ‘post’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category’,
‘terms’ => array( $scat ),
),
);
// query
$query = new WP_Query( array( ‘cat’ => $args ) );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
$content = apply_filters(‘the_content’, $post->post_content);
<?php
if( have_rows(‘chapter’) ) {
while( have_rows(‘chapter’) ) {
the_row();
$row = array(
‘link’ => $content ,
);
add_sub_row(‘link’, $row);
}
}
?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
update*
<?php
$scat = get_the_term_list( $id_product,”scat”);
$args = array(
‘post_type’ => ‘post’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category’,
‘terms’ => array( $scat ),
),
);
// query
$query = new WP_Query( array( ‘cat’ => $args ) );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
$content = apply_filters(‘the_content’, $post->post_content);
<?php add_product_meta($product_id, ‘chapter’, $content); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
******
it still doesn’t work 🙁
update*
it still doesn’t work thou
<?php
$scat = get_the_term_list( $id_product,”scat”); // I add this ‘scat’ through the product page in Admin Panel by using Custom Taxonomy. It will be the ID of the wordpress category */
// query
$query = new WP_Query( array( ‘cat’ => $scat ) );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
$content = apply_filters(‘the_content’, $post->post_content);
<?php add_post_meta($post_id, ‘chapter’, $content); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
I don’t think there is a function you can call to do this. When you set the save/load terms options for a taxonomy field ACF calls WP functions with the right info to make this happen.
When saving the field ACF calls wp_set_object_terms() https://codex.wordpress.org/Function_Reference/wp_set_object_terms
When loading the field ACF calls wp_get_object_terms() https://codex.wordpress.org/Function_Reference/wp_get_object_terms
You can get more information about what ACF is doing by looking at the file for the taxonomy field /advanced-custom-fields-pro/includes/fields/class-acf-field-taxonomy.php.
I think that in order to do what you want that you’ll need to call these WP functions yourself.
Hi @puur
Demonstrated in this Taxonomy doc, you could change the line in your code where you create the “li” and echo $ook to be something like:
<li><?php echo $ook->name; ?></li>
In the same code on the above linked page, it also shows how to get the link and description.
Hope that helps!
Keith
Hi @hube2
Thanks for the code but I am getting the Parse error. Check below
Parse error: syntax error, unexpected '<' in /home/ee8bu/public_html/elkeesdeals.com/lib/skins/child/taxonomy.php on line 49
To better understand the issue, I am sending the taxonomy PHP file through attachment.
I’m also looking to solve a similar problem: I would like a select field to populate some text fields with related data. (My Select field is populated with custom taxonomy items, each of which have custom fields. I want their custom field data to populate the text fields.).
Any luck with this?
Hi @eyesx
Sorry for the late reply..
Yes.. creating terms is an option for the Taxonomy field in the Pro version (Added in v5.2.3). I didn’t realize myself that it wasn’t in the free version. However, the free version just released with an optional v5 upgrade, so you can play more with the free version before committing if you like, because it has the feature you are after.
@mkeys After playing around with it a bit more, I’m not sure if it is Groups or it could be duplicative terms I had with custom taxonomies and ACF fields.
ACF deals with this well by attaching the key to any field action to avoid namespace collision so you can use the same field name over again (e.g. within repeater fields or groups).
My guess is WordPress is not liking the way I had things set up.
For example, if I have a custom post type of Locations with taxonomy slugs of ‘airport’, ‘train-station’, ‘hotel’ then I had a Group named ‘hotel’. With ACF normally this wouldn’t be a problem but I think it is confusing WP.
No, there isn’t a field for tags (taxonomy) that has a search like you are looking for. But if you use a select field for display this field does have a basic ability to search where if you start typing in the first characters of the tag name it will jump to the first one that matches.
Hi there…
When you are creating a Date field, you can choose the “display” format, and the “return” format independently… there is a Custom option where you can put the mask you specified above.
For taxonomy… you can choose a return type of Object, rather than ID.
Then, to output the taxonomy name, you would do:
(assuming a ‘single’ choice field like a radio or select button)
<?php
$term = get_field('qicname');
if( $term ) {
echo $term->name;
}
?>
For a multi-choice taxonomy, you would create a loop as shown here:
https://www.advancedcustomfields.com/resources/taxonomy/
No other plugins are installed.
This issue is getting stranger as time goes on. On my test environment (http://acftax.wpengine.com/?p=1), those taxonomy fields randomly started working. However, on my staging environment, they’re obviously still broken. I tried switching themes to Twentyseventeen, re-adding my custom fields and taxonomies, and adding the acf_form
snippet, and instead of “The results could not be loaded”, I got “No results found”
Are their arguments that might be required with register_taxonomy
for custom post type taxonomies to work correctly with ACF’s taxonomy field inside acf_form
?
I ran into this need today and put together a plugin that allows for selection of taxonomy fields and terms in the conditional rules.
https://github.com/mattkeys/ACF-Conditional-Taxonomy-Rules
Currently this is written only for ACF v5. I am unsure what it would take to also support v4 and if I plan to do that.
It should be as simple as install, activate, and start selecting taxonomy fields and terms as needed in your conditional fields.
Try it out and let me know if you run into any bugs! If it works well for others I will likely release this on the WordPress plugin repository (Unless Elliot wants to roll this functionality into core.
easy if you already know all of the field values. print_r($field)
A taxonomy field returns an array of term objects. You need to loop through the array. There are code examples here https://www.advancedcustomfields.com/resources/taxonomy/
Was it that easy?? 🙂 damn…
One other issue i hit, i made a checkbox list containing taxonomy.
I cant show the values. The result i get is a text “Array” instead of the actual value/label selected.
I tried all possible codes i could find in this forum, but no solution.
Hi James, I have resolved this…who says ACF can’t handle this sort of thing 😉
I’ll post this on the forum as well just in case anyone wants to see it there. First things first, this is the code I ended up with and I will try to explain it:
<?php $args = array (
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
);
$terms = get_terms( 'category', $args );
foreach ( $terms as $term ) {
echo '<br />';
echo '' . '<img src="' . get_field('category_image', $term->taxonomy . '_' . $term->term_id) . '">';
$post_args = array (
'category_name' => $term->slug,
'posts_per_page' => '-1',
'no_found_rows' => true
);
$query = new WP_Query( $post_args );
while ( $query->have_posts() ) {
$query->the_post(); ?>
<br /><div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
<?php }
} ?>
In its simplest terms this code display a category image field created using ACF and all the pages assigned to the categories. Note these are pages, not posts!
You will see in this code that I have applied <div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
Firstly the hidepost class can be used in CSS to hide all the pages that are now loaded into every page sidebar in the site.
You would just use:
.hidepost {
display:none;
}
in your stylesheet.
Secondly we have a class named: page-id-<?php echo $post->ID; ?>
This pulls in the page id class so you end up with something like page-id-10 which can be used to show or hide the page link something like:
.page-id-10 {
display:block!important;
}
You can of course do this from the stylesheet but I needed to go a step further and have the display of these pages controlled through the admin, which is where ACF came in use again.
I created a repeater field named Sidebar Links with a field in the repeater called Page Link which utilises the Page Link Field Type.
This would normally output a url which was no use in my case as I needed it to output the page ID of the Page Link. So I delved into the ACF plugin itself and edited fields>page_link.php (I have kept a backup of the original).
In page_link.php find around line 603:
// convert $post to permalink
if( is_object($post) ) {
$post = get_permalink( $post );
}
In mine I changed it to:
// convert $post to permalink
if( is_object($post) ) {
echo '.page-id-';
echo $post->ID;
echo '{';
echo 'display:block!important;';
echo '}';
$post = ( '' );
}
In my template I can now use:
<?php
// check if the repeater field has rows of data
if( have_rows('sidebar_links') ):
// loop through the rows of data
while ( have_rows('sidebar_links') ) : the_row();
the_sub_field('page_link');
endwhile;
else :
// no rows found
endif;
?>
which outputs something like .page-id-10 {display:block!important;}
So if I want to control the display of each field dynamically I can now apply this code into the header.php template in the <head></head> section like this:
<style>
<?php
// check if the repeater field has rows of data
if( have_rows('sidebar_links') ):
// loop through the rows of data
while ( have_rows('sidebar_links') ) : the_row();
the_sub_field('page_link');
endwhile;
else :
// no rows found
endif;
?></style>
The above allows me to display the page link if it is selected using the ‘edited’ page link field.
Ok I now have this which displays the category image and all the posts associated with the category beneath it `<?php $args = array (
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘hide_empty’ => true,
);
$terms = get_terms( ‘category’, $args );
foreach ( $terms as $term ) {
echo ‘<br />’;
echo ” . ‘<img src=”‘ . get_field(‘category_image’, $term->taxonomy . ‘_’ . $term->term_id) . ‘”>’;
$post_args = array (
‘category_name’ => $term->slug,
‘posts_per_page’ => ‘-1’,
‘no_found_rows’ => true
);
$query = new WP_Query( $post_args );
while ( $query->have_posts() ) {
$query->the_post(); ?>
<br /><div class=”hidepost”><?php the_title(); ?></div>
<?php }
} ?>`
What I now need is a way to show or hide the page links on a page by page basis.
Anybody have any ideas how I might achieve this?
I’m not sure that I understand completely what you mean but if you’re talking about the main WP Admin menu on the left. This would be accomplished where you are adding those admin pages. If your using ACF to add options pages, then you would set the capability required to access the options page. When creating a custom post type or taxonomy, these functions also have capability settings that would be used.
For standard WP admin pages you can try this https://www.wpmayor.com/how-to-remove-menu-items-in-admin-depending-on-user-role/
Well got a couple of bits of code sort of working:
Firstly this one which may be good for development further:
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<aside id="secondary" class="sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php $terms = get_terms( 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . ' - ' . '<img src="' . get_field('category_image', $term->taxonomy . '_' . $term->term_id) . '"></li>';
}
echo '</ul>';
} ?>
This retrieves the category image for each category. I can build on that because I could apply the category image into a div background. This would hide the category image if there are no links to show beneath if I apply the image into the div as a background and the div doesn’t have a height.
I just don’t know how to pull the posts in that would appear beneath each category heading.
Then there is this:
<?php
$args = array(
'post_type' => 'page','posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<img src="';
the_field('category_image', 'category_4');
echo '">';
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>
This lists a category image above each post. If you look at the code I am having to call a category id – the_field(‘category_image’, ‘category_4’);
to get the image to display.
This would mean the same category image would display for all categories which would be wrong. On the other hand this retrieves all posts and puts them under the correct category titles they should appear under.
So if I could somehow combine the above two pieces of code I would be halfway there.
Although I still have to find some way of choosing which pages display from each category under each heading in the sidebar. I was thinking perhaps set them all somehow as ‘false’ until chosen from each page in the admin from a custom field menu then set them as ‘true’ if chosen.
Both examples of code can be viewed here:
http://151.252.3.6/~lewishickeyplatf/wordpress/
You will see Code 1 Example and Code 2 Example
Code 1 being the first bit of code above and Code 2 being the second bit of code above.
Thanks for reply maira,
Yes the field name is $foto. but its not working.
Actually i want to add a label on the Product category images like this
http://prntscr.com/gefwfd
I have set the setting below way…
1. Set file name $foto
2. Return Value set to Image URL http://prntscr.com/geft8t
3. set rule, Taxonomy Term -> is equal to -> Product Categories
http://prntscr.com/gefrjw
4. The Image showing ok on the product category fields http://prntscr.com/geg1s2
5. added your code to functions.php
if ( is_product_category() ) {
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$foto = get_field( 'foto', $queried_object );
$foto = get_field( 'foto', $taxonomy . '_' . $term_id );
if( get_field('foto', $taxonomy . '_' . $term_id) ) {
echo '<div class="cat-banner"></div>';
}
}
i am a newbee, Sorry for bothering you. Would you like to suggest me what else need to do.
Thanks in advance
RESOLVED
I mistakenly added the code for the ‘acf/update_value’ to the product taxonomy page. After moving it to the functions.php it worked.
Okay,
I played around some more, and this seems to be a bug/inconsistency related to the ambiguous ways you can call wp_nav_menu
, in particular the menu attribute you can pass in, which can be:
(int|string|WP_Term) Desired menu. Accepts (matching in order) id, slug, name, menu object.
Here the different output for different ways of calling my menu:
wp_nav_menu(array('menu' => 'Footer menu'));
results in print_r($args->menu)
:
stdClass Object ( [menu] => Footer menu [container] => div [container_class] => [container_id] => [menu_class] => menu [menu_id] => [echo] => 1 [fallback_cb] => wp_page_menu [before] => [after] => [link_before] => [link_after] => [items_wrap] =>
%3$s
[item_spacing] => preserve [depth] => 0 [walker] => [theme_location] => )
Where as this:
wp_nav_menu(array('menu' => 1));
results in print_r($args->menu)
:
stdClass Object ( [menu] => WP_Term Object ( [term_id] => 3 [name] => Footer menu [slug] => footer-menu [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw ) [container] => div [container_class] => [container_id] => [menu_class] => menu [menu_id] => [echo] => 1 [fallback_cb] => wp_page_menu [before] => [after] => [link_before] => [link_after] => [items_wrap] =>
%3$s
[item_spacing] => preserve [depth] => 0 [walker] => [theme_location] => ) MENUWP_Term Object ( [term_id] => 3 [name] => Footer menu [slug] => footer-menu [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw )
So apparently the menu will render with both methods, but the $args->menu
differs significantly, and in the string parameter variant does not result in the term object ACF is expecting. Hurray WordPress.
I think you may be something else incorrect, but I can’t tell what it is from what you’ve provided.
When using this
add_filter('wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2);
function my_wp_nav_menu_items( $items, $args ) {
...
$args->menu
should be a term object. When I add this filter and do print_r($args)
I get this:
stdClass Object
(
[menu] => WP_Term Object
(
[term_id] => 30
[name] => manue 1
[slug] => manue-1
[term_group] => 0
[term_taxonomy_id] => 30
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
)
[container] => div
[container_class] =>
[container_id] =>
[menu_class] => nav-menu
[menu_id] => primary-menu
[echo] => 1
[fallback_cb] => wp_page_menu
[before] =>
[after] =>
[link_before] =>
[link_after] =>
[items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
[item_spacing] => preserve
[depth] => 0
[walker] =>
[theme_location] => primary
)
Looking at this https://www.advancedcustomfields.com/resources/adding-fields-menu-items/
add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
function my_wp_nav_menu_objects( $items, $args ) {
...
$args results in the same.
Probably a little known fact, but $post_id when calling ACF functions can also be an object of type post, term or user.
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.