Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • Hi @dawood

    Thanks a lot for reaching out to us.

    You would to have to use the standard WP query to query for posts which belong in that taxonomy.

    Hope this helps.

  • The value of $post_id depends on what version of ACF you are using.

    In ACF 4 or < 5.5.0

    
    $image = get_field('cck_menu_category_image', $category->taxonomy.'_'.$category->term_id );
    

    In ACF >= 5.5.0 you can use the above or you can use

    
    $image = get_field('cck_menu_category_image', 'term_'.$category->term_id );
    

    I think that you might also be able to use (but I’m not sure)

    
    $image = get_field('cck_menu_category_image', $category);
    
  • ok, having read through what you have put several times so its clear in my head I have to left of the : put the id of the term, Ie 36 : Embedded Systems. I made the change above to the code to reflect it as ID and it is still not setting the taxonomy on save. So I am now very much lost, is this the way I defined the custom taxonomy?

  • Then your values (left part of : ) need to have the exact name/value as the name of your taxonomy.

  • Sorry just re-read your question. You need to get the number of posts with a difficulty of Easy, Average & Difficult for each term right?

    You’ll need a custom wp_query with a tax_query and a meta_query.. Something like the below will get you the post count for Easy Chocolate Deserts..

    
    $args = array(
    	'post_type'  => 'recipes',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'desserts',
    			'field'    => 'slug',
    			'terms'    => 'chocolate',
    		),
    	),
    	'meta_query' => array(
    		array(
    			'key'     => 'difficulty',
    			'value'   => 'Easy',
    			'compare' => 'IN',
    		),
    	),
    );
    $easy_choc_query = new WP_Query( $args );
    
    echo $easy_choc_query->found_posts;
    

    More info from the Codex

    To make it more dynamic we’d need to see your html structure. You’d possibly need to put something like the above inside the get_terms foreach loop, swapping out ‘chocolate’ and ‘easy’ for variables that changed with each iteration.

  • You can use get_terms, then term->count..

    The below will list all the terms from the Desserts taxonomy with their name and post count.

    <?php   
    $terms = get_terms( 'desserts' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        echo '<ul>';
        foreach ( $terms as $term ) {
            echo '<li>' . $term->name . ' - ' . $term->count . '</li>';
        }
        echo '</ul>';
    }
    ?>
    

    For more info check – HERE

  • You have to define your options as “value : label, where value is then your taxonomy id. For example, for Sex I have 3 options:
    1 : Woman
    2 : Man
    3 : Trans
    This returns an id.

    Whereas if you define it like this:
    woman
    man
    trans

    or

    woman : [anything except the word woman]
    man : [anything except the word man]
    trans : [anything except the word trans]

    then it returns a string.

    So it depends on what value $posted_roles is.
    If it’s an ID, you need to change this
    $term_id = get_term_by( 'name', $posted_roles, 'asproducts_cats' );
    to
    $term_id = get_term_by( 'id', $posted_roles, 'asproducts_cats' );

    which should then match your taxonomy id of course.

    So how did you define your options ?

  • Hi I am trying to get either of the two above examples to work and seem to be failing> I am not sure what is going wrong. This is the code

    function change_post_taxonomy_44582( $post_id ) {
        // bail if no ACF data
        if ( empty($_POST['acf']) ) {
            return;
        }
        // get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
        $stored_role = wp_get_post_terms($post_id, 'asproducts_cats');
        // get submitted value from acf form
        $posted_roles = $_POST['acf']['field_59154e83efb0e'];
        // get term_id for the submitted value
        $term_id    = get_term_by( 'name', $posted_roles, 'asproducts_cats' );
        // if stored value is not equal to posted value, then update terms
        if ( $stored_role != $posted_role ) {
            wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
        }
    }
    add_action('acf/save_post', 'change_post_taxonomy_44582', 20);
    

    Have I done something wrong?

  • I’ve done last night something very similar to this, a custom hierarchical taxonomy for med. anal. cat.; then created promos as a cpt, using same taxonomy, and a relationship with a hospital ctp; only thing different is that there, in promo, I linked through another relationship to a lab test cpt (every promo has one lab test related; and a lab has many promos, depending on the hospital). But I’m affraid of the performance, because I will use a lot of queries 🙁

  • Rather than a repeater field I probably would probably have created a custom hierarchical taxonomy for medical analysis categories. Promos would then be another CPT that uses the same taxonomy and has a relationship field with the hospital CPT.

  • @cargoodrich since it’s a taxonomy field you would need to use the filter provided by the taxonomy field to alter the call to get_terms https://www.advancedcustomfields.com/resources/acffieldstaxonomyquery/. To be honest I don’t know the exact solution.

  • Hi John,

    Could you please advise in light of your suggestion above with 5.5.x as to how I can modify the below to change the ordering?

    
    $sub_categories = get_field('featured_categories');
    foreach ($sub_categories as $sub_category):
        echo $sub_category->name;
    endif;
    

    sub-category is a Taxonomy field with the return value as Term Object

  • +1 on this, but in the mean-time, I’ve created a workaround. Simply use a “Taxonomy” as a select or radio (single value) field, and to get a link to that taxonomy archive, use:
    get_category_link( get_field( 'your-taxonomy-field' ) )

    for example, I used it in this way:
    printf( '<a href="%1$s">Your Category</a>', get_category_link( get_field( 'your-taxonomy-field' ) ) );

  • Pulsecode,
    if you need an automate solution try my plugin scan on every acf post save for unused acf or you can complete delete all before save and “resave” the field.

    Still need some work but as far as i test works, you can also open a issue on github to improve it.
    Works at the moment only on post and custom post type not on the taxonomy but when i have time i will improve the feature.

    https://github.com/filippozanardo/redvolver-cleanacf

  • Glad to say i’ve solved it myself.

    In stead of pulling post_categories, i had to pull the terms. Since its in a custom post / taxoomy, I had to pull the object_terms in stead of the post terms.

    So I’ve got my terms via $term = get_term_by('id', $dateCatId, 'gemeente'); , gemeente being my taxonomy.

    Next problem was that my wp_get_object_terms read the ID as a string and created a new category with the id as slug. Thats why i pull the ID back out on the term like this:
    wp_set_object_terms($post_id, $term->term_id, 'gemeente');

    Hope i’ve helpen someone with this, and also, if anyone has a better alternative, glad to hear it!

    function my_acf_save_post($post_id) {
    
        // bail early if no ACF data
        if (empty($_POST['acf'])) {
            return;
        }
    
        //array of category id's
        $categoryIds = array();
    
        //value of 'date' field is the cat_id
        $dateCatId = get_field('organisatie');//http://www.advancedcustomfields.com/resources/get_field/
        array_push($categoryIds, $dateCatId);
    
        $term = get_term_by('id', $dateCatId, 'gemeente');
    
        wp_set_object_terms($post_id, $term->term_id, 'gemeente');
    }
    
    // run before ACF saves the $_POST['acf'] data
    //add_action('acf/save_post', 'my_acf_save_post', 1);
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • ok, the above is actually working for posts and pages if i use the image field

    but not for taxonomies (I’ve added an image field to taxonomy terms, so I can have a nice hero for taxonomy archives)

    think i need to change this

    $image = array( ‘src’ => get_field(‘product_image’));
    wp_localize_script( ‘digital-backstretch-set’, ‘BackStretchImg’, $image );

    to work with this

    // vars
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;

    // load thumbnail for this taxonomy term (term object)
    $thumbnail = get_field(‘thumbnail’, $queried_object);

    // load thumbnail for this taxonomy term (term string)
    $thumbnail = get_field(‘thumbnail’, $taxonomy . ‘_’ . $term_id);

    but not sure how

    at the moment I’m getting the img showing in backstretch but no src…

    any help appreciated !

  • Hello everyone,

    I was looking at this and it is exactly what I am looking to do but with a multiselect taxonomy field.

    What would be the procedure? Would it be possible to sum up the working solution?

    Thanks for your help.

  • Hi @kikadesign

    For single value Taxonomy selection, you can set the return type to “Term Object” and then make use of this code:

    function create_title( $post_id ) {
        // Set variables
        $term = get_field( 'ministry_name', $post_id );
        $name = $term->name;
    
        // Set post title and permalink
        $post_title = $name;
        
    
        // update the post, which calls save_post again
        wp_update_post( array(
            'ID'            => $post_id,
            'post_title'    => $post_title,
            'post_name'     => $post_name,
            )
        );
    }
    
    add_action( 'acf/save_post', 'create_title', 20 );
  • I rewrote my code to use WP_Query but unfortunately this method returns all posts instead of posts assigned to a specific category.

    <?php
    
    $args = array(
        'post_type'      => 'cars',
        'posts_per_page' => -1,
        'taxonomy' => 'car_category',
    
    );
    
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
    
          <a href="<?php the_permalink(); ?>">
            <?php echo the_post_thumbnail(''); ?>
          </a>
    
        <?php endwhile; ?>
    
    <?php else : ?>
    
    <p>Sorry, no products found.</p>
    
    <?php endif; ?>
    
    <?php wp_reset_postdata() ?>
    
    <?php wp_reset_query() ?>
  • Hi @brevalessio

    Thanks for the post.

    I would recommend that you pass the actual $term object like so:

    function selectArchetype()
    {
      $terms = get_terms([
          'taxonomy' => 'archetype',
          'hide_empty' => false,
      ]);
    
      foreach ($terms as $term) {
        $fields = get_field('must_have_cards', $term);
        var_dump($fields);
        }
    }
  • I have done this very thing for a client (but for different reasons)…

    Here’s the code I used to display a WYSIWYG custom field in the attributes list:

    add_filter( 'woocommerce_get_product_attributes', 'ariel_display_recordings_attribute', 20 );
    function ariel_display_recordings_attribute( $attributes ) {
    	$ariel_recording_details = get_field('ariel_recording_details');
    	if (!empty(get_field('ariel_recording_details'))) {
    
    	$attribute = array(
    		'name'         => 'Recording',
    		'value'        => $ariel_recording_details,
    		'is_visible'   => '1',
    		'is_taxonomy'  => '0',
    		'is_variation' => '0',
    		'position'     => '0',
    	);
    
    	$attributes['ariel_recording_details'] = $attribute;
    	}
    
    	return $attributes;
    }

    You can also add additional code to attributes.php to specify what order you’d like attributes to appear in…

  • Hi John!!

    Great solution!!

    I didn’t get any values to $taxonomy & $category->term_id so I changes $taxonomy to category and $category->term_id to $category

    $value = get_field(‘featuredimage’, category . ‘_’ . $category);

    if ($url) { -statement was very good. I added default image on CSS if category image hasn’t been set.

    John, I really appreciate you helping me!

  • 
    <?php 
    
    // added to single.php
    // if outside of "The Loop" we need to get the current post id
    $queried_object = get_queried_object();
    $post_id = $queried_object->ID;
    
    // gets a list of all post categories
    $categories = wp_get_post_categories($post_id);
    // you could also limit the list returned above by including
    // secong $args parameter for function, for more info see
    // https://developer.wordpress.org/reference/functions/wp_get_post_categories/
    
    $url = false; // initialize $url value
    
    if (count($categories)) {
      // the post has one or more categories
      // loop through them and see if any has an image set for the field
      foreach ($categories as $category) {
        $value = get_field('featuredimage', $taxonomy.'_'.$category->term_id);
        if ($value) {
          // this category has an image
          // you could also do more checking here
          // if there are other conditions to
          $url = $value;
          // in this case, use the first category that has an image
          // once we find an image stop looking
          break;
        } // end if $value
      } // end foreach categories
    } // end if categories
    ?>
    <div id="banner"<?php 
      if ($url) {
        ?> style="background-image: url(<?php echo $url; ?>);"<?php 
      } // end if url
      ?>>
    
  • HI @aengstrom

    This should be possible by appending a tax_query in a WP_Query loop to filter the posts using the assigned terms. The code would look like this:

    $args = array(
    	'post_type' => 'ebh_locations',
    	'tax_query' => array(
    	array(
    		'taxonomy' => 'category',
    		'field'    => 'slug',
    		'terms'    => array('addiction')
    	),
    	),
    );
    $query = new WP_Query( $args );

    For more information, have a look at: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • Hi @artisancodeur

    Thanks for the clarification. To change the text displayed on the admin, you can hook into acf/fields/taxonomy/result filter and then fetch the values of the custom fields containing the price.

    The code would look something like this:

    <?php
    
    function my_relationship_result( $title, $term, $field, $post_id ) {
    	
    	// load a custom field from this $object and show it in the $result
    	$price = get_field('price', $post->ID);
    	
    	
    	// append to title
    	$title .= ' [' . $rice .  ']';
    	
    	
    	// return
    	return $title;
    	
    }
    
    // filter for a specific field based on it's name
    //add_filter('acf/fields/taxonomy/result/name=my_taxonomy', 'my_relationship_result', 10, 4);
    
    ?>
Viewing 25 results - 1,776 through 1,800 (of 3,193 total)