Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • Instead of using two separate things for actor, post type and the taxonomy, you can use the custom field with ACF in movies and set the rules as “Post Type” equal to actor, then you don’t have to enter the actor names twice.

    Also querying will become easier, because both of them will be linked to each other.

    And then what you can do in the actor page is loop through the movies and use ‘get_field’ to find out which movies a particular actor is connected with.

  • Hi,

    I’m not using any specific code for this as mailster uses the custom field slug to show the content.

    What i have tried was changing some options when creating the custom fields.
    Here are the settings i’ve tried:

    Field Type = Taxonomy
    Taxonomy = custom taxonomy created with CPT UI
    Appearance = selection
    Create Terms = yes
    Save Terms = yes
    Load Terms = yes
    Returned value = Object Term

    And

    Create Terms = no
    Save Terms = no
    Load Terms = no
    Returned value = Object ID

    When i choose the returned value to be the object term, nothing loads. So i was wondering if iam missing something while creating the custom field.

    PS: If it helps when i use the the_field(‘field-slug’); function, it also returns the ID instead of the term.

    Thanks

  • Under location rules for the field group.

    ACF 5: Taxomomy is equal to category
    ACF 4: Taxonomy Term is equal to Categories

  • This is kinda complicated as a custom field could be attached to something else than a post type. It can be attached to a specific page, a taxonomy or a lot of other things. In fact it is attached to all the posts which correspond to the type of object(s) you attach the custom field.

    That’s why (I think) the get_field_object() requires at least one $post_id to be found.

  • Looks like I spoke too soon. The above works perfectly if the taxonomy is set a select menu (only shows the 2 categories), but if i set to checkbox, radio buttons, etc, it lists all the categories rather than just the two.

    Is this a bug, or do I need to add something extra? Also is there anyway to set a default category value for new posts with this method?

  • You’re missing a slash

    'acf/fields/taxonomy/query/name=event_category'

    https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/

  • Been struggling with this as well. Based on the above using the following in hopes of having my ACF category field only show two categories, the ones with an id of 10 and 16. The field name is “event-category”. When using the below in functions.php, it does not take any affect so assuming I’m missing something. Any thoughts? Also is there a way to set either 10 or 16 to be checked by default on new posts in the custom post type?

    function training_taxonomy_query( $args, $field, $post_id ) {
    $args[‘include’] = ‘10,16’;
    return $args;

    }

    add_filter(‘acf/fields/taxonomy/queryname=event_category’, ‘training_taxonomy_query’, 10, 3);

  • Just in case anyone else has the same issue, I solved it by upgrading to version 5. The taxonomy works as expected in this version.

  • Thanks John,it worked like a charm – I’ve created a multi-select field called “acf-user-schools” and added this:

    function acf_load_user_school_field_choices( $field ) {
    	
        // reset choices
        $field['choices'] = array();
        
    	
                   // Get all the schools hierarchy ( Country -> District -> School )
    	$schools  = getTaxonomyHierarchy( SCHOOLS_TAXONOMY );
    	
    	// Get the children (all the districits) of USA
    	$schools  = $schools [ USA ]->children;
    	
    	if ( !empty( $schools  ) AND !is_wp_error( $schools  ) ) 
    	{
    		foreach( $schools as $district ) 
    		{  
    			$field['choices'][ $district->term_id ] = $district->name;
    		}
    	}
    
        return $field;
        
    }
    add_filter('acf/load_field/name=acf-user-schools', 'acf_load_user_school_field_choices');
  • This solution didn’t work for me. I made the post author id 1 or the admin and got the same result AFTER spending a LONG time trying to make my “guest author” have the right capabilities without success. I am using CPT UI to make my taxonomies and post types. For not logged in users save_post was my solution…

    Make your own inputs with whatever values you want:

    $htmlAfter = '<input type="hidden" name="tax_name" id="tax_name" value="ENTER_MAIN_TAXONOMY_SLUG">';
    $htmlAfter .= '<input type="hidden" name="tax_slug" id="tax_slug" value="ENTER_INDIVIDUAL_TAXONOMY_SLUG">';

    Add those inputs to acf_form function with html_after_fields parameter (or html_before_fields):

    'html_after_fields' => $htmlAfter

    Save those values ($_POST) as you wish with save_post action:

    function my_acf_save_post( $post_id ) {
        
       if(isset($_POST['tax_name']) && isset($_POST['tax_slug'])) {
    	 wp_set_object_terms( $post_id, $_POST['tax_slug'], $_POST['tax_name'], true );
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • $htmlAfter = '<input type="hidden" name="tax_name" id="tax_name" value="ENTER_MAIN_TAXONOMY_SLUG">';
    $htmlAfter .= '<input type="hidden" name="tax_slug" id="tax_slug" value="ENTER_INDIVIDUAL_TAXONOMY_SLUG">';

    'html_after_fields' => $htmlAfter

    function my_acf_save_post( $post_id ) {
        
       if(isset($_POST['tax_name']) && isset($_POST['tax_slug'])) {
    	 wp_set_object_terms( $post_id, $_POST['tax_slug'], $_POST['tax_name'], true );
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    I know it might not be 100% related, but this idea could be applied in MANY ways, considering where ever you include acf_form, you could use whatever variables are available and then include that data in input values and then include those inputs in the html_after_fields or html_before_fields parameter for the acf_form function.

  • Wanted to give some help. I was stuck on this part for a while to. When you use acf_form() you NEED acf_form_header() FOR SURE if the person is not logged in (unsure about admins logged in)… BUT the save_post method is good. It ISN’T capabilities that is messing it up. I tested to be sure by making the author in acf_form() be the admin (all capabilities) and that didn’t solve the problem. So the save post method is the best BUT then it is hard to get OTHER variables outside the post you created (like from the page you used acf_form() on etc):

    
    $htmlAfter = '<input type="hidden" name="tax_name" id="tax_name" value="ENTER_MAIN_TAXONOMY_SLUG_HERE">';
    	$htmlAfter .= '<input type="hidden" name="tax_slug" id="tax_slug" value="ENTER_INDIVIDUAL_TAXONOMY_SLUG_HERE">';

    Then include it in the acf_form() via the parameter “html_after_fields”:

    'html_after_fields' => $htmlAfter

    THEN, the save_post function:

    function my_acf_save_post( $post_id ) {
        
       if(isset($_POST['tax_name']) && isset($_POST['tax_slug'])) {
    	 wp_set_object_terms( $post_id, $_POST['tax_slug'], $_POST['tax_name'], true );
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    You could enter the values for taxonomy and slug manually or set them as variables conditionally based on whatever you want, where ever you include acf_form() …

  • When getting values for anything other than a post you must supply the correct $post_id value to ACF so it knows where to get the value from. For terms it is explained here https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/ and all of the different values that might be needed are explained in the get_field() documentation here https://www.advancedcustomfields.com/resources/get_field/

  • An alternative approach is to install https://wordpress.org/plugins/taxonomy-terms-order/, order the terms how you want via drag and drop and then the terms automatically appear in the new custom order.

  • I figured this out, where in the code they have:
    $terms = get_field(‘taxonomy_field_name’);

    I had named my ACF taxonomy term and thus was using taxonomy_term for the field_name.
    That was causing the issue.

  • You need to edit that plugin so that the function it’s talking about is compatible with the core version of the same method. I don’t know what would be involved in doing that.

    It actually looks like there is a pull request that would fix this issue https://github.com/GCX/acf-taxonomy-field/pulls

  • Well, the weird part was caused by my code, it seems 🙂

    This is how it works: ACF is checking the manage_terms capability.

    At first I didn’t understand because manage_terms is not listed here:
    https://codex.wordpress.org/Roles_and_Capabilities

    It’s also not available in the User Role Editor plugin, which I was using to assign capabilities to roles.

    But manage_terms is a special capability that is created when you use register_taxonomy() to register a taxonomy:
    https://codex.wordpress.org/Function_Reference/register_taxonomy

    It is set up as a reference to another capability, in the example it is manage_categories. But in my case I had a custom taxonomy where the manage_terms was set to edit_users for some reason, and that explains it all!

  • Circling back to this to provide a solution for someone who may be looking for it.

    I had three custom post types before:
    product
    container
    scent

    I changed the container and scent from custom post type to taxonomy.

    Again, the goal was to put content in a format that I could pull, based on the product custom field values (using ACF custom fields).

    Thus: Lavender Mason Candle $20

    Where “Lavender Mason Candle” is the product, “lavender” is the scent, and “mason candle” is the container. The $20 comes from a custom field on the container taxonomy.

    Solution (this is for a taxonomy.php page, but the main part at the bottom could very well work on others.)

       <ul>
    	<?php
    		// Get the current page taxonomy term info
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 	
    										
    		// args
    		$args = array(
    			'post_type'   => 'products', //
    			'tax_query'   => array(
    				array(
    					'taxonomy' => 'scents',
    					'field'    => 'slug',
    					'terms'    => $term,
    				),
    			)
    		);
    
    		// New Query
    		$custom_query = new WP_Query( $args );?>
    					
    		<?php if( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    				  	
    			<li>
    				<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    				<!-- For part of the title, I want to temporarily pull info from the $container -->
    				<a href="<?php the_permalink(); ?>" title="<?php the_title();?>: $<?php $container = get_field('container'); if( $container ): the_field('base_price', $container); ?>+<?php endif; ?>">
    				<!-- After the title, I want to revert back to the original post data-->					
    					<p><?php the_title();?></p>
    					<!--Now back again to the container-->	
    					<?php $container = get_field('container'); if( $container ): ?>
    					<p class="price">$<?php the_field('base_price', $container); ?>+</p>
    					<?php endif; ?>
    					<!--And back again to the original post data-->
    				</a>
    								
    			</li>
    		<?php endwhile; wp_reset_postdata();?>
    </ul>

    This is the main worker to help selectively pick what goes from where:

        <?php $container = get_field('container'); if( $container ): ?>
    					<p class="price">$<?php the_field('base_price', $container); ?>+</p>
    					<?php endif; ?>

    Hope this helps someone else and saves them three days of searching for answers for something similar.

  • 
    // current term
    $current_term = get_queried_object();
    
    // child terms
    // this returns an array of terms
    $args = array(
      'taxonomy' => 'YOUR TAXONOMY HERE',
      'parent' => $current_term->term_id,
      // you may need other arguments depending on your needs
    );
    $child_terms = get_terms($args);
    
    // you need to maybe loop through the child terms gotte
    // to pick which one you want to use
    // I'm assuming that you only want to use the first one
    
    $child_term = false; // set it to false to begin with
                         // we'll use this later
    if ($child_terms) {
      $child_term = $child_terms[0];
    }
    
    // make a decision
    if ($child_term) {
      // get field value(s) from child term
      $color = get_field('color', $child_term);
    } else {
      // get field value(s) from current term
      $color = get_field('color', $current_term);
    }
    
    // do something with the values
    echo $color;
    
    
  • Think I have worked this out using a foreach loop from get_terms and then test to see if the post is in the category still with in_category

    $types = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => true,
    ) );
    
    foreach($types as $type) {
        
        $image = get_field('jg_category_tag', 'category_' . $type->term_id . '' );
        
        if( in_category($type->term_id )) {
            echo '<img src="' . $image['url'] . '" /> ';
        }
    }

    Now the posts in the loop will display the categories images only if they belong to the category.

  • Just to be sure, you do understand, where it says {taxonomy}/{term_id} you have to replace it with ‘your variables’ ?

    echo get_field('custom_category_description', category_categoryID );

  • Try to changed in to
    echo get_field('custom_category_description', {taxonomy}_{term_id});

    But give the error 500, and also not working after disable all plugin, is it possible cause by WP upgrade, this happened after version upgraded

  • So I worked out out to show the Taxonomy image in a single or archive template of a post that belongs to the category..

    $image = get_field('jg_category_tag', 'category_5');
    
        if ( in_category( 'my-category' ) ) {
            echo '<img src="' . $image['url'] . '" /> ';
    }

    So i use a conditional check to see if the post belongs to the category and if it does it displays the category image.

    But now what is the more optimal way of writing the code when I have numerous categories with images and to only display when a post belongs to them.

    The code below would go into the loop but is not efficient…

    // vars
    $image = get_field('jg_category_tag', 'category_5');
    $image7 = get_field('jg_category_tag', 'category_7');
    $image8 = get_field('jg_category_tag', 'category_8');
    
    if ( in_category( 'my-category' ) ) {
        echo '<img src="' . $image['url'] . '" /> ';
     }
    if ( in_category( 'your-category' ) ) {
        echo '<img src="' . $image7['url'] . '" /> ';
    }
    if ( in_category( 'another-category' ) ) {
        echo '<img src="' . $image8['url'] . '" /> ';
    }
    
  • Since posting this bug a new version of ACF was released. I updated ACF but the problem persisted.

    To continue troubleshooting I deactivated all other plugins on the site. This did not fix the issue. I then switched to the Twenty Seventeen theme. I now saw the option to set rules around Taxonomies.

    Now knowing that this was an issue with my theme, I checked the debug log but found that it was empty. I then reactivated my theme and discovered that I was still able to see the Taxonomy location to set rules. One by one I reactivated my plugins and could still set up taxonomy rules.

    So… the short is that I don’t know what was causing the issue but updating ACF, deactivating all my other plugins, switching to another theme temporarily, then activating everything fixed my issue.

  • I’m not really sure about all of the permissions needed, or even all the details to make it work. I’ve actually never used a taxonomy field on a front end form.

Viewing 25 results - 1,551 through 1,575 (of 3,193 total)