@hube2
Solved with your help, today I did the function I needed! Thanks!
I write a piece, maybe someone can help.
The function takes the title of a post-type related to products (woocommerce) and inserts this title as a woocommerce attribute when save_post action !
add_action('acf/save_post', 'update_post_meta', 20);
function update_post_meta( $post_id ) {
$post_type = get_post_type($post_id);
global $product;
if ( 'product' == $post_type ) {
//TAKE RELATED INFO IN POST TYPE
$posts = get_field('serie_di_appartenenza');
if( $posts ):
foreach( $posts as $p ):
$scrittore=get_field("scrittore_appartenenza", $p->ID);
if( $posts ):
foreach( $scrittore as $s ):
$scrittore_ereditato= get_the_title( $s->ID );
//ADD INFO IN GLOBAL ATTRIBUTE WOOCOMMERCE
$term_taxonomy_ids = wp_set_object_terms( $post_id, $scrittore_ereditato, 'pa_scrittore-woo', true );
endforeach;
endif;
endforeach;
endif;
//ADD INFO IN CURRENT POST
$data = Array(
'pa_scrittore-woo'=>Array(
'name'=>'pa_scrittore-woo',
'value'=>$scrittore_ereditato,
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
),
);
//UPDATE META
update_post_meta( $post_id, '_product_attributes',$data);
}
}
it may have something to do with WC and this may not be returning what I think it should be returning
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
I don’t know how to test this. Taxonomy should be ‘product_cat’ (i think that the WC taxonomy name) and term_id should be the id of the category.
Since you’re saving and loading terms you may need to use a tax_query, but this is only a guess.
<?php
// get the current category ID
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// query posts
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, // or whatever number you want to show
'tax_query' => array(
array(
'taxnomy' => $taxonomy,
'terms' => array($term_id)
)
)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$my_query->the_post();
// show post
}
wp_reset_postdata();
}
The value of $args is the same thing you’re using in $terms = get_terms(['taxonomy' => 'yst_prominent_words', 'hide_empty' => true]);
what you need to return here is the arguments that ACF will use when it calls get_terms()
.
But if you only want to show terms in a single taxonomy why not just set that taxonomy in the Filter by Taxonomy
setting for the field?
You’ve probably figured this out. The cause of this is that the plugin you mention changes the name of the field from post_category[]
to radio_tax_input[category][]
. ACF is not detecting the category because it can’t find the category field. Using this plugin you will need to save the changes to the post for the correct field groups to appear.
There may be a way to add to alter what ACF is submitting in the AJAX request in order to use the value set for this field. Unfortunately I’m not familiar enough with this to know if it can be done or not and it would probably take some extensive digging through the JS to figure it out.
Alternately you might be able to use a single field group with a taxonomy field that only allows one selection and to use conditional logic for the remaining field in the group.
Another possible solution is to not use the plugin your using and construct a custom callback for showing the category selection box that does not alter the name of the field used, unfortunately I’m not sure about this either. I would probably start by figuring out how the plugin that you’re using is defining the custom callback function and then use that in combination with my own meta box function that does not alter the field name. For a radio button you would just use post_category
instead of post_category[]
Okay, so the selected taxonomy itself is stored as the ID in the custom field. ACF lets you decide how you want the info returned. So I just need to search for the ID.
Thank you so much,
That worked at first try!
I do have a feeling I could have made the code more efficient. Setting variables twice?
Or is it correct right now. (it works 😉 )
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
add_action('pre_get_posts', 'uitgelichte_cursusloop_land');
function uitgelichte_cursusloop_land($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_tax($taxonomy )) {
$query->set('meta_key', 'uitgelicht_bij_land');
$query->set('meta_value', '1');
$query->set('posts_per_page', 30);
}
}
// The Query
$the_query = new WP_Query(
array(
'post_type' => 'cursus',
'posts_per_page' => 30,
'meta_key' => 'uitgelicht_bij_land',
'meta_value' => '1',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => array($term_id)
)
)
)
);
// The Loop
if ( $the_query->have_posts() ) {?>
<h3>Uitgelichte taalcursussen in <?php single_term_title(); ?></h3>
<?php while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'cursusloop', get_post_format() );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata(); ?>
I have the same issue – also trying to order by a custom date field. So the code looks almost identical. I have orderby written correctly but still have this issue.
I’ve also tried ‘orderby’ => ‘meta_value’ and ‘orderby’ => ‘meta_value_num’ with no difference.
My args are:
$args = array(
‘post_type’ => array( $post_type ),
‘post_status’ => array( ‘publish’ ),
‘posts_per_page’ => -1,
‘tax_query’ => array(
array (
‘taxonomy’ => $taxonomy,
‘terms’ => $term_id,
)
),
‘meta_query’ => array(
array(
‘key’ => ‘date’,
‘compare’ => ‘>=’,
‘value’ => $date_now,
‘type’ => ‘DATETIME’
),
),
‘order’ => ‘DESC’,
‘orderby’ => ‘meta_value’,
‘meta_key’ => ‘date’,
‘meta_type’ => ‘DATETIME’,
);
I’d be grateful if you have any pointers 🙂
if all you want to show on this page is the featured posts and the rest of the posts in the term will never be shown then instead of doing a custom query on the page you need to use a pre_get_posts filter and alter the query that WP is already doing.
add_action('pre_get_posts', 'your_function_name_here');
function your_function_name_here($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_tax('your-taxonomy-here')) {
$query->set('meta_key', 'uitgelicht_bij_land');
$query->set('meta_value', '1');
$query->set('posts_per_page', 3);
}
}
If on the other hand you are showing other posts as well then you do need to do a custom query, but you need to add a tax_query. You can get the current taxonomy like
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
then you can use these values in the tax query
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => array($term_id)
)
)
Thanks for all the quick replies. Still no luck, though.
Removing the standard meta boxes is what the code in my first post was doing (provided it was correct code). I do have to say, though. The taxonomy meta boxes did not appear in the first place, until I used the following code in my functions file.
function kw_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'kw_add_tags_to_attachments' );
This is what adds the standard textbox that just takes the slug. But I removed this once I started working with the ACF taxonomy fields.
Have you removed the standard meta boxes for the taxonomy? This might be helpful but I don’t know. https://support.advancedcustomfields.com/forums/topic/fields-not-working-on-image-attachments-details/
Thanks @hube2, but this did not yield a solution.
I’ve actually found that it actually doesn’t even save the tags I set when I do it from the attachment page and click “update”. It would be really great to figure this out because the ACF fields for taxonomy are much better than the default WordPress textbox to type the taxonomy slug into.
I’m experiencing the same issue. Choosing any (custom) taxonomy causes the fields to gain ‘acf-hidden’.
No WPML, WP 4.9.8, ACF PRO 5.7.6.
These settings cause the taxonomy field to work the same way as the default taxonomy metabox that you removed. It updates the relationships of posts to terms
Create Terms: Let’s users create new terms
These are the ones that will give you problems
Load Terms: Gets existing terms the post is associated with
Save Terms: Saves the terms for the post
With these off the only place these terms are associated are in the acf field and you must to a meta_query to get posts in a specific term
With these turned on the value are stored in a way that will let you create wordpress term templates “taxonomy-{$taxonomy_name}.php” and use a tax_query for getting posts in a term.
I am experiencing the same issue and reverting back does not seem to resolve the issue due to the ACF database update.
Choosing a taxonomy causes the acf-postbox to gain the class acf-hidden.
You would have to use a taxonomy field.
the problem is that you are looking at the actual term currently set for the post that is stored by WP.
$main_post_cat = noir_get_team_term($options['post_id'], "team_category", 'slug');
You need to look at this, but you also need to check to see if ACF has included the currently selected therm in the $options
that it is passing to the filter. This is actually more important then testing the posts current terms, but you need to do both, first checking the $options
and if it is not set or it is empty then checking against the post’s current terms.
If nothing has changed since the last time I created a custom location rule then the terms selected will be in $options['taxonomy']
One of my old examples shows this https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-post-category-ancestor-location-rule.php
Oh, I have found solution faster than I thought.
I must use acf/save_post
but with 20 priority which runs after saving $_POST data.
I’m taking subcategory value from post, checking if that term exist and add it to post using wp_set_post_terms
.
Code:
function my_acf_save_post_after($post_id) {
require_once('wp-load.php' );
require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
// bail early if no ACF data
if (empty($_POST['acf'])) {
return;
}
//get post value
$parent_cat2 = get_field('curr_categories', $post_id);
$test_cat2 = get_field('cat_test', $post_id);
$term_id = term_exists( $test_cat, 'category', $parent_cat2 );
wp_set_post_terms( $post_id, $term_id, 'category', true );
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
I thought I should add more details.
Here are my investigations :
There is a screen capture of my post edition page attached.
I found a json file in my theme that seems to deal with categories in an ACF-jon folder :
{
"key": "group_5ac5d1e87ab94",
"title": "Categorie",
"fields": [
{
"key": "field_5ad704be7333f",
"label": "Afficher la catégorie courante",
"name": "afficher_les_articles_dans_la_page_categorie",
"type": "true_false",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"default_value": 1,
"ui": 1,
"ui_on_text": "",
"ui_off_text": ""
}
],
"location": [
[
{
"param": "taxonomy",
"operator": "==",
"value": "categorie"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"modified": 1530175641
}
And finally that’s what I tried to add in function.php with no success :
add_filter('body_class', 'add_acf_body_class');
function add_acf_body_class($class) {
$value = get_field('afficher_les_articles_dans_la_page_categorie', false, false);
$class[] = $value;
return $class;
}
Could you clarify?
So in the acf group, create your fields.
Then in your rules set “post taxonomy” to equal the taxonomy you want the fields to show for.
When creating your posts, if you checkbox the taxonomy/category you chose the fields will show.
To troubleshoot:
1. check that the fields are showing further down the page, sometimes they will show but they will only appear at the bottom of the post unless you set the priority
2. Try saving the post to see if the fields show
Post a screen shot of your rules so we can see 🙂
@hube2 I don’t get it to work. When i select taxonomy under forms and select the product categories i get my fields. However when i select the product cat with post taxonomy it doesn’t show up. Could you help me out here?
My issue was resolved in 5.7.4. I believe it was related to the WPML + Taxonomy field bugfix noted in the release notes.
Could you explain how you registered this taxonomy? Odds are it’s not a public taxonomy for one reason or another and it’s being excluded from the list.
I have gotten the image url to show back up if I give the full path to a single Attribute.
add_action(‘woocommerce_single_product_summary’,”finish_image”,22);
function finish_image() {
echo ‘<b>Finish:</b>’. get_field(‘finish_img’, ‘pa_metal-finish_79’);
}
But I need the code to search though the metal finish attributes to find the one that applies to that Item.
I know its something like the post found here.
https://support.advancedcustomfields.com/forums/topic/get-field-from-woocommerce-attribute-taxonomy/
but I cant seem to quite figure it out, do you have any insite?
I’m not sure if ACF has a field like this ( not that I could see in the basic version anyway ) but this type of thing would work best as a Non Hierarchical Taxonomy with a Select2 type field. Think post tags: it’s searchable, you can assign multiple, and it’s listed out in a formatted easily manageable input for the user to digest. You can control whether the user will be able to create new terms or only select from existing terms. You can populate with AJAX and give them a populated list of options. It seems to make sense for the usecase described.
Terms is pretty efficient when querying because of the relational tables already setup. A custom table and query may be more efficient but would also be more work to setup.
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.