Hi,
I have created a text field and the taxonomy is set to product_tag (Woocommerce tag page). The position is set to after content.
Would would the snippet be to show the text field below the products in the tag page?
I have a few of customizations for my WP editor:
* custom color swatches (in a specific order)
* 5 JS plugins to do things like create placeholders, insert icons or shortcodes, ect
* a ton of custom formats
* various other visual components via CSS
these are all added via WP hooks like “tiny_mce_before_init”, “mce_buttons_4”, and “mce_external_plugins” and these word fine for the regular EP editors. These also work as expected for ACF WYSIWYG fields BUT ONLY when a WP editor field is also on the page.
If I put a WYSIWYG field on an options page, or a taxonomy term edit page then none of my custom options are there. If you use Automattic’s “Advanced Editor Tools” plugin to say add a font family button or add font colors – those buttons WILL appear, but they will be missing any custom settings added via “tiny_mce_before_init”.
Looking at the WP source code, it looks like WP takes the user settings and spits them out as a JS object and adds that the the JS to build the tinymce instance: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-editor.php#L1566
It doesnt seem that this is run for the ACF instances of the editor. I put a var_dump & die statement in there which will print on an edit post page but will NOT print on say an option page with the ACF editor in it.
I have a custom post type, “Trade Alerts”, which is registered via code. It uses ACF fields with location set to that post type. All is good there. I’ve created three taxonomies for this CPT: Pair, Trade Status, and Trade Type. These taxonomies already exist as ACF fields mentioned above. What I am trying to do is use them as taxes instead of just custom fields.
What I’ve done
– I’ve registered all three as taxonomies via code.
– I’ve set the code to show them in the admin columns
– I’ve set them to visible in quick edit for those I wanted
– I’ve used acf/load_field/
to populate terms from the custom taxonomy
Where I’m stuck
New meta boxes, one for each taxonomy, are now visible on the Trade Alerts edit page, but I want to use the already existing ACF fields to set these values for each post. I realize they can be hidden via screen options but that doesn’t solve it.
I’ve got those fields being populated by the custom taxes, but the fields do not save with the post. It’s expecting the values from the new meta boxes – how do I get my new custom taxonomies’ values to be stored via the already existing ACF fields mentioned?
Here is my code:
Taxonomy: Trade Status
function cptui_register_my_taxes_trade_status() {
/**
* Taxonomy: Trade Status.
*/
$labels = [
"name" => __( "Trade Status", "Avada" ),
"singular_name" => __( "Trade Status", "Avada" ),
];
$args = [
"label" => __( "Trade Status", "Avada" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => false,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'trade_status', 'with_front' => true, ],
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "trade_status",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => true,
"show_in_graphql" => false,
];
register_taxonomy( "trade_status", [ "tradealert" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_trade_status' );
/**
* Populate the ACF field with terms from the custom taxonomy Trade Status type.
*/
add_filter( 'acf/load_field/name=trade_status', function( $field ) {
// Get all taxonomy terms
$trade_status_options = get_terms( array(
'taxonomy' => 'trade_status',
'hide_empty' => false
) );
// Add each term to the choices array.
// Example: $field['choices']['review'] = Review
foreach ( $trade_status_options as $option ) {
$field['choices'][$option->slug] = $option->name;
}
return $field;
} );
Taxonomy: Pair.
function cptui_register_my_taxes_pair() {
/**
* Taxonomy: Pair.
*/
$labels = [
"name" => __( "Pair", "Avada" ),
"singular_name" => __( "Pair", "Avada" ),
];
$args = [
"label" => __( "Pairs", "Avada" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'pair', 'with_front' => true, 'hierarchical' => true, ],
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "pair",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
"show_in_graphql" => false,
];
register_taxonomy( "pair", [ "tradealert" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_pair' );
Taxonomy: Trade Types.
function cptui_register_my_taxes_trade_type() {
/**
* Taxonomy: Trade Types.
*/
$labels = [
"name" => __( "Trade Types", "Avada" ),
"singular_name" => __( "Trade Type", "Avada" ),
];
$args = [
"label" => __( "Trade Types", "Avada" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => false,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'trade_type', 'with_front' => true, ],
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "trade_type",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => true,
"show_in_graphql" => false,
];
register_taxonomy( "trade_type", [ "tradealert" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_trade_type' );
/**
* Populate the ACF field with terms from the custom taxonomy Trade Status type.
*/
add_filter( 'acf/load_field/name=type', function( $field ) {
// Get all taxonomy terms
$trade_type_options = get_terms( array(
'taxonomy' => 'trade_type',
'hide_empty' => false
) );
// Add each term to the choices array.
// Example: $field['choices']['review'] = Review
foreach ( $trade_type_options as $option ) {
$field['choices'][$option->slug] = $option->name;
}
return $field;
} );
Trade Alert CPT
function cptui_register_my_cpts_TradeAlert() {
/**
* Post Type: Trade Alerts.
*/
$labels = [
"name" => __("Trade Alerts", "Avada"),
"singular_name" => __("Trade Alert", "Avada"),
"menu_name" => __("Trade Alerts", "Avada"),
"all_items" => __("All Trade Alerts", "Avada"),
"add_new" => __("Add Trade Alert", "Avada"),
"add_new_item" => __("Add New Trade Alert", "Avada"),
"edit_item" => __("Edit Trade Alert", "Avada"),
"new_item" => __("New Trade Alert", "Avada"),
"view_item" => __("View Trade Alert", "Avada"),
"view_items" => __("View Trade Alerts", "Avada"),
"search_items" => __("Search Trade Alerts", "Avada"),
"not_found" => __("No Trade Alerts Found", "Avada"),
"not_found_in_trash" => __("No Trade Alerts found in Trash", "Avada"),
];
$args = [
"label" => __("Trade Alerts", "Avada"),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => ["slug" => "trade-alert", "with_front" => false],
"query_var" => true,
"menu_icon" => "dashicons-location-alt",
"supports" => [
"title",
"editor",
"thumbnail",
"excerpt",
"revisions",
"page-attributes",
"custom-fields"
],
"taxonomies" => [ "pair", "trade_status", "trade_type" ],
];
register_post_type("Trade Alert", $args);
}
add_action('init', 'cptui_register_my_cpts_TradeAlert');
I have a page that throws a loop of a custom post Type (stores), I created a taxonomy (store_the_genre) which is divided in Men, Women, Children for the type of clothing, I want to know how to use the Post Query to post the genre
//Protect against arbitrary paged values
$testMe = get_field('store_the_genre');
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
if(have_posts() ):
$args = array( 'post_type' => 'store',
'posts_per_page' => 11,
'paged' => $paged,
'meta_key' => 'store_client_type',
'tax_query' => array(
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $style,
),
array(
'taxonomy' => 'bgenre',
'field' => 'term_id',
'terms' => $testMe,
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'city_clause' => array(
'key' => 'store_city',
'value' => $villeshow,
'compare' => 'IN'
),
),
),
);
$loop = new WP_Query( $args );
while($loop->have_posts()): $loop->the_post();
Hello all,
I have create a custom taxonomy named Campaigns.
Each campaign contain a number of custom post type, Offer.
Each offer is belong to a custom post type named Restaurant. There has a Post Object field for select which restaurant the offer is belong to in Offer custom field group.
I would like to know how can I only use wp_query() is good enough to get a group of offers and ordered by the restaurant post title?
Thank you.
Setting up something for a client where the taxonomy may frequently update. Have seen some situations where changing the items in a select field (I think) meant that all previous posts using that field had to be re-saved when a new value is added later. That would be a disaster in this case.
So I’m curious what might be the most flexible field when new selections get added?
Or would it be better to use a custom taxonomy in combo with radio buttons? Fairly flexible in terms of the interface, but requires stability so that when they add new categories, no previous posts would be affected.
Hi, I am working with a custom category taxonomy field named ‘category-tax’, that returns Term ID. Having a tough time figuring out how to get the parent category returned, when the parent category is designated in the post. It seems I am only getting the parent category returned when the child category is designated.
I am working with what appears to be non-native ACF code, it sort of functions with this exception but would like to clean this up so it works as intended. Wondering if anyone could shed some light on how to sort this out?
function getCategoryButtons() {
$postCategories = get_the_category($postID);
$postHTML .= '<div class="buttons">';
if ($postCategories[0]->parent != 0) {
$cat = get_category($postCategories[0]->parent);
$postHTML .= '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name.'</a>';
}
$postHTML .= '</div>';
return $postHTML;
}
If I copy and paste in the following code which I copied and pasted from the ACF site, it returns all parent and child categories so I know the field is working.
$terms = get_terms( 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . ' - ' . get_field('short_description', $term->taxonomy . '_' . $term->term_id) . '</li>';
}
echo '</ul>';
}
Hello! I have a true/false option field. If the option is set to 1, I’d like to have my custom plugin register a taxonomy. Here’s my current code, however, it’s not registering the tax. What might I be doing wrong here? Thanks in advance for any assistance!
function wed_get_vendor_enable() {
$enablevendortax = get_field('enable_vendor_tax', 'option');
if($enablevendortax='1') {
/**
* Taxonomy: Vendors.
*/
function register_my_cpts_wed_vendors() {
$labels = array(
"name" => __( "Vendors", "wed" ),
"singular_name" => __( "Vendors", "wed" ),
);
$args = array(
"label" => __( "Vendors", "wed" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => false,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'vendors', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "wed_vendors",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
);
register_taxonomy( "wed_vendors", array( "post", "wed_vendor"), $args );
}
}
}
add_action( 'init', 'wed_get_vendor_enable' );
Hello,
what i need is to let my client select featured posts of categories in his site. For this I added a local field group in my plugin code.
array(
'key' => 'field_6172b1e27f165',
'label' => 'Featured cat posts',
'name' => 'featured_cat_posts',
'type' => 'post_object',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'post',
),
'taxonomy' => array(
0 => 'category:'.$slug,
),
This is real magic.
Testing with
$slug = "clients";
i’m able to select articles of may category “clients”.
This dynamic code fails
$catinfo = get_category( $tagID );
$slug = trim( $catinfo->slug );
even if the array is the same
'taxonomy' => array(
0 => 'category:clients',
),
Is there any wizard of oz function to get this working ?-))
I trie almost everything to be sure that this respects the format.
Cheers and thanks for all your ideas and helping
Christian
Hello ACFers,
We have created a custom product taxonomy named Author (product_author) on a woocommerce project.
In this taxonomy we have created a custom field named creator, which have different types of authors like poet, write etc.
We have found a way to display this fields only on this specific page by calling them with the code
$field = get_field_object( 'creator' );
$value = $field['value'];
esc_attr($value);
We want also to display this field in the product page but we haven’t figure out how.
We are trying to use something like this $field = get_field_object(‘my_field’, 123) but it’s not working because the number 123 is representing posts id and not tag_ID that is used for taxonomies.
Is it possible to display this fields in other pages?
please implement https://wpfavs.com/plugins/conditional-taxonomy-option
thanks
Hello,
Need help with some problem,
Cannot get custom fields group in the frontend of the vendor if I use Location Rules
Post type = Product and
Post Taxonomy = product_cat “Cars”
On plugin, settings have the option to show but just if I use
Post type = Product
if I add another rule they won’t show files.
I find on the plugin front end page some code and need to add this taxonomy (what i don’t know make)
public function set_props( $id ) {
$this->id = $id;
$this->product = wc_get_product( $this->id );
$this->post_type = get_post_type( $this->id );
$this->post_taxonomy = get_post_taxonomy( $this->id ); //add a ‘Post Taxonomy’ filter
$this->field_groups = acf_get_field_groups(array(
'post_id' => $this->id,
'post_type' => $this->post_type,
'post_taxonomy' => $this->post_taxonomy
));
}
and I get an error on get_post_taxonomy well…
I have no clue how to register or write this get_post_taxonomy and will really appreciate if anyone can help, if not well thank you anyway 🙂
Thank you
Hello,
I’m trying to order repeater field in admin area according to my needs e.g. certain array of taxonomy IDs.
In repeater field i’m displaying meal times (taxonomy) with checkboxes and meals inside given meal time e.g:
breakfast, second breakfast, dinner, supper
However, order is incorrect which causes that supper is first, breakfast is last which causes ux confusion.
I’ve started with acf_load_value but without any success.
Order I want to achieve is visible here: https://i.imgur.com/BNBSzJx.png
I’d be grateful for any guidance how to achieve such thing
I have made a function that runs before i save a custom post type. This function i want to make some calculations inside and update the fields on the custom taxonomy that belongs. Is there a way to do so?
I’m trying to show / hide a field based on the checked value of a taxonomy field – found this plugin that does the trick https://wpfavs.com/plugins/conditional-taxonomy-option but I do not like to rely on some third party code – do you have. any idea how to do this with just ACF Pro?
thank you
I have a Field Group Repeater that works within a Custom Post Type.
I also have Custom Taxonomy that is easily picked up by the Repeater, i.e. when I select the Custom Taxonomy I can – so all good.
The Custom Taxonomy is “US States” that lists out the US States….all simple stuff…..
However, I have two questions regarding things that don’t work:
If I select a State from within the Custom Taxonomy then ACF Repeater easily finds, for example “New York”, however, it is then not listed as being “counted” in the actual Taxonomy itself. What I mean is that if you visit the actual Custom Taxonomy Page the New York page has “0” despite the fact that it has been selected
Also, is it possible to list or loop attributed posts in that Custom Taxonomy
Hope that all makes sense and thanks for all replies!
Couldn’t figure this for weeks. Two custom post types. One has taxonomies. The other one is a post object of the first one.
I want to show all custom post types from the second one, BUT from the taxonomy which is under the first one.
Example:
Artist CPT: Title = Mike. (taxonomy – > location, term – > europe )
Event Title (relation field/post object )
————————————————-
Event Lorem Ipsum 1 (relation field/post object “Mike”)
Event Lorem Ipsum 2 (relation field/post object “Mike”, “Robbie”)
Event Lorem Ipsum 3 (relation field/post object “Mike” , “Andrea”)
Event Lorem Ipsum 4 (relation field/post object “Mike”, “Max”, “Antonio”)
Now I want to show all the latest published events in general from the taxonomy location, term = europe.
Also Inside each event, i want to show an artist title which here would be Mike.
So event would have an event title, date, and artists related to that event.
Hi all,
I have a custom post type “testimonials” that has a few fields added to it via ACF Post Object. I call them related fields – they are not set up as a taxonomy, but they act like taxonomy in this situation. They are actually other custom post types. They are country and continent.
These related fields are set in ACF to appear in posts that are testimonials, and that is working great. Example:
Testimonial title: Ben’s Great Trip
Date: 10/10/21
Related Country: Kenya
Related Continent: Africa
See screenshot: https://snipboard.io/EMUZ3F.jpg
I am building a custom block, for which everything is already setup and in place, but I’m stuck correctly writing query. The block should show the latest testimonial posts that have a specific related field, newest first, with pagination optional.
So my block would have
– a field to choose which related field (a Post Object unless you have a better idea)
– a field to choose how many testimonial posts
– a field to optionally set a limit (only show the latest 5, etc)
So typical use would be to add this block to the Kenya page, to display the testimonials with Kenya in their Country field.
I’ve gotten stuck writing the query, but am familiar with most of the parameters. I’ve spent a lot of time duplicating other blocks on this site that do similar things, but haven’t gotten it right yet.
The code below works, except my custom field does not show up.
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$featured_img = wp_get_attachment_image_src( $post->ID );
$countryTestimonial = get_field('testimonial_country'); ?>
<h3><?php echo $countryTestimonial; ?></h3>
<?php print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();
Sometimes there is more than one country added to a testimonial post, as i underlined in that screenshot above, so that’s probably part of the problem – the related fields are arrays sometimes having one but maybe more countries added.
Do i need to add the related custom fields in the $ars
section instead of where i did in the code above? Do i need to somehow search the array to find Kenya (in this case)?
Hello, I have a custom taxonomy and add entries to this taxonomy programmatically with data I get from an API. The custom taxonomy has some acf fields I also fill in this process.
This API doesn’t have data on some fields in some entries. So with data from another API I want to iterate over the taxonomy again and only fill the empty fields.
When I just do the same import process with the second API, update_field will update the empty fields but also change the data of the fields that already have data from the first API.
So, I want to check If a field is empty before update_field. Is there a function which checks if a field is empty? I couldn’t find a solution yet. I tried with
if(get_field($term_id) === '') {
update_field($key, $value, $term_id);
}
But it is not working…
Does anyone have a suggestion?
BR
Not sure what I’m doing wrong. Have a taxonomy select in a repeater, where I’m trying to allow the user to select their category for a list. But it always resets back to the first available category after saving.
Have Load and Save Terms and using Term Object for the return. Everything loads in the front end, but it simply won’t save any selections.
Hi,
I know there’s an option to create custom conditions like: https://www.advancedcustomfields.com/resources/javascript-api/#acf.condition-extend
But these conditions work based on fields within the currently edited field group, so if one field has value of something then show / hide current field.
I’m looking for a solution that would enable to write conditions based on current post properties like: if a post has assigned taxonomy of slug equal to something then show / hide this field
.
Can this be solved similarly simple as the acf.condition-extend or would it need much more work to accomplish?
`I’m trying to figure a way how to make posts per page work. By default shows all. I’ve tried a bunch of ways but no luck even with putting a second query inside the foreach loop.
From acf documentation https://www.advancedcustomfields.com/resources/relationship/
<?php
$eventposts = new WP_Query( array(
‘post_type’ => ‘artist’, // This is the name of your post type – change this as required,
‘posts_per_page’ => 4,
‘order’ => ‘DESC’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘location’, // taxonomy name
‘field’ => ‘term_id’, // term_id, slug or name
‘terms’ => 836, // term id, term slug or term name
)
)
) );
while ( $eventposts->have_posts() ) : $eventposts->the_post();
// The content you want to loop goes in here:
?>
<?php
$featured_posts = get_field(‘relation’);
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
<span>A custom field from this post: <?php the_field( ‘field_name’ ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Hi,
I have added 2 fields in the product categorie section where i can add own written text. Setting is set: Taxonomy is equal to categorie (product_cat). Those fields are visable in the backend product categorie. Now I want to show them on my site using the shortcode [acf field="category_content"], but it doesn’t show this field.
What am i doing wrong?
I have a Field Group Repeater that works within a Custom Post Type.
I also have Custom Taxonomy that is easily picked up by the Repeater, i.e. when I select the Custom Taxonomy I can – so all good.
The Custom Taxonomy is “US States” that lists out the US States….all simple stuff…..
However, I have two questions regarding things that don’t work:
If I select a State from within the Custom Taxonomy then ACF Repeater easily finds, for example “New York”, however, it is then not listed as being “counted” in the actual Taxonomy itself. What I mean is that if you visit the actual Custom Taxonomy Page the New York page has “0” despite the fact that it has been selected
Also, is it possible to list or loop attributed posts in that Custom Taxonomy
Hope that all makes sense and thanks for all replies!
Thanks
Hello everybody,
I found this “Display location address details” on
Display location address details
This is real cool.
Now I try to extract information and put it directly into my taxonomy LOCATION and CITY.
But how do I put the info $location[ ‘city’ ] and $location[ ‘state’ ] into the Taxonomies.
function my_copy_date_filter( $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type != 'spot' ) {
return;
}
$location = get_field( 'spot_location', $post_id );
if( $location[ 'lat' ] && $location[ 'lng' ] ) {
update_field( 'spot_lat_lng', $location[ 'lat' ].','.$location[ 'lng' ], $post_id );
}
if( $location[ 'state' ] ) {
update_field( 'new_spot_state', $location[ 'state' ], $post_id );
}
if( $location[ 'city' ] ) {
update_field( 'new_spot_city', $location[ 'city' ], $post_id );
}
// HOW CAN I PUT $location[ 'city' ] and $location[ 'state' ] into taxonomy
}
add_filter( 'acf/save_post', 'my_copy_date_filter', 20 );
Any suggestion?
Thanks in advance,
Denis