Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • I created a taxonomy ‘marques-produit’ for my CPT ‘produits’. I used ACF to add the field image ‘logo_marque’ on my taxonomy.
    Now I want to display on my front page the field ‘logo_marque’ for every label like this
    <img src="<?php echo esc_url($logo['url']); ?>" alt="<?php echo esc_attr($logo['alt']); ?>" />
    Thank you

  • So I’ve changed my code :

    add_action('acf/save_post', 'save_commune');
    function save_commune( $post_id ) {
        global $post; 
        $cat_id = wp_create_category( $post->post_title, 0 );
        $field = get_field_object('cat_id');
        update_field($field['key'], $cat_id, $post_id );
    }

    But unfortunately when I save the post with a title. It still shows an empty select for my Taxonomy field. I need to manually refresh the page to see the selected value.

  • I also have fields that don’t save the first time. I use them in custom fields with a taxonomy condition. Is there any update on this?
    The acf fields for another custom post type – without conditions – do save at first save.
    But my ACF image-field in the taxonomy categorie don’t show also not on a Xth save – no conditions apply here.

  • All I can to you tell you how this would be done in PHP. If you’re using some type of page builder theme then you need to contact them.

    
    // example, loop on single post page
    if (have_posts()) {
      while (have_posts()) {
        the_post();
        $terms = get_post_terms($post->ID, 'your-taxonomy');
        if ($terms) {
          $term = $terms[0];
          $image = get_field('image_field_name', $term);
        }
      }
    }
    
  • I just try this code in my functions.php to see if it have any effects on taxonomy field without any result ? That very strange. Or I do not do it correctly.

    add_filter('acf/fields/taxonomy/query', 'my_acf_fields_taxonomy_query', 10, 3);
    function my_acf_fields_taxonomy_query( $args, $field, $post_id ) {
    
        // Show 40 terms per AJAX call.
        $args['number'] = 40;
    
        // Order by most used.
        $args['orderby'] = 'count';
        $args['order'] = 'DESC';
    
        return $args;
    }
  • https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/

    I don’t know if it’s possible with a taxonomy field. You can try setting
    $args['number'] = 0;

  • Hello

    it’s a “Taxonomy” field or a “Taxonomy Terms” field.

  • Thanks! Was looking for exactly that.

    My use case is an automatic generation of field groups based on registered option pages.
    I’m going to ‘connect’ a Taxonomy to an attachment and needed a UX-friendly way of doing so. So I’ll be making a sort of link table in an option page. Having a Taxonomy dropdown, and a file-field or a media selection field.

    Anyways, I was bummed to find out that acf_get_options_pages() is undocumented and at the moment, I’m not sure if $GLOBALS['acf_options_pages'] is always there. Either way this was exactly what I needed and seems to work flawlessly in ACF Pro 5.11.4.

  • My solution in case someone wants an example:

    // FILTER FOR PREMIUM CONTENT FOR COMPANIES
    
    add_filter('acf/fields/taxonomy/query', 'moxie_acf_company_premium', 10, 3);
    function moxie_acf_company_premium( $args, $field, $post_id ) {
    	
    	$args['taxonomy'] = 'category';
    	
        $args['meta-query'] = array(
    		array (
    			'key'		=> 'premium',
    			'value'		=> 'yes',
    		)
    	);
    
        return $args;
    }
  • Yes, something like that could work, but it depends on how many posts you have.

    
    add_action('acf/init', 'update_post_year_field');
    update_post_year_field() {
      $args = array(
        'post_type' => 'your-post-type',
        'post_status' =>'any',
        'posts_per_page' => -1,
        'meta_query' => array(
          'relation' => 'OR',
          array(
            'key' => 'year'
            'compare' =>'NOT EXISTS'
          ),
          array(
            'key' => 'year'
            'value' => '',
            'compare' =>'='
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)) {
        foreach ($query->posts as $post) {
          $terms = wp_get_post_terms($post->ID, 'your-taxonomy');
          if (!empty($terms)) {
            // use the field key here, not the field name
            update_field('field_key', get_field('item_year', $term[0]), $post->ID);
          }
        }
      }
    }
    

    If you have a lot of posts it may timeout the loading of your site. This is why the query is looking for the field not being set or has an empty value. The number of posts returned should be reduced each time and eventually it will complete and then you can remove the filter.

    But you will also need to create an acf/save_post filter to update this field when a new post is created or a post is updated.

  • Thanks, John.

    “If you’ve changed themes and you are still seeing ACF fields (do you mean options pages), then whatever is being done could be done in a plugin.”

    No, these are ACF taxonomy links (and “empty” custom post types fly-outs) in the main WP Admin menu (down the left column of WP Admin) which have nothing to do with the new site theme or plugins. I can see from the “live” site (using ACF) what these all relate to, but there aren’t needed in the new theme site.

    I understand from reading the forum that it can be very problematic removing un used ACF fields. I’d be very wary about removing anything directing in the DB. :-0 🙂

  • Hmm,

    If events is the custom post type, then this isn’t right:
    'category_name' => 'events',

    Your meta query seems to have 2 values you’re passing in:
    'meta-value' => $value,
    'value' => $today,

    What if you combine everything into one query, something like:

    
    $args = array(
    	'post_type'			=> 'event_cpt',			
    	'posts_per_page'	=> 6,
    	
        'orderby'    => 'meta_value_num',
        'order'      => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'event_start_date',
                'value'   => $today,
    			'type' 	  => 'DATE',
                'compare' => '>=',
            ),
        ),
              	
    );
    
    $args['meta_query'] = array( 'relation' => 'AND' );	
    
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', #AND
    
    	array(
    		'taxonomy'	=> 'academic_programs',
    		'field'		=> 'term_id',
    		'terms'		=> $academic_programs_id
    	),			
    		
    );
    
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', #AND
    
    	array(
    		'taxonomy'	=> 'event_type',
    		'field'		=> 'term_id',
    		'terms'		=> $event_type_id
    	),			
    		
    );
    
    $query = new WP_Query( $args );
    if( $query->have_posts() ) : 
    
    	while( $query->have_posts() ): $query->the_post(); ?>
        	<span><?php $date = get_field('event_start_date');
    		$starttime = get_field('event_start_time');
    		echo $date . " - " . $starttime; ?></span></br> 
    	<?php endwhile;  wp_reset_postdata();
    
    endif;

    Code is totally untested.
    Also worth adding debugging code in:

    global $wpdb;
    
    // Print last SQL query string
    echo $wpdb->last_query;
    
    // Print last SQL query result
    echo $wpdb->last_result;
    
    // Print last SQL query Error
    echo $wpdb->last_error;

    You can see what the output of your query contains, it may then point you in the right direction

  • Thanks John.

    So I made it works as Dropdown List, do you know how ?

    I saved my Taxonomy field as Radio Button (that works).
    Then re-saved my Taxonomy field as Dropdown List and tadaaa! Works fine.

    I don’t explain why. Previously I saved multiple times the field Taxonomy field without changing the Dropdown List setting and it was not solving the problem.
    But switching to radio button -> Save, then choose Dropdown -> Save make it works.

  • When using a select field for taxonomy fields it uses Select2. An AJAX request is done to get the terms. If loading fails it means that the AJAX request is returning something other than what is expected. This could be because of an error on the server or it could be caused by some output that should not be happening.

    If you add custom JS you can inspect the ajax data before and after the request and this might lead you to what the issue might be https://www.advancedcustomfields.com/resources/javascript-api/#filters-select2_ajax_data

  • Can you show your code please?

    And to clarify, the taxonomy is called floor4
    This is linked to a CPT of flat4
    The ACF field is called hover and it’s this value you’re trying to get?

    Does this work:

    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'flat4',
    	'paged' 		=> $paged,
    	'fields' 		=> 'ids'			
    );
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    
    		$taxonomies = get_terms( array(
    			'taxonomy' => 'floor4',
    			'hide_empty' => false
    		) );
    
    		if ( !empty($taxonomies) ) :
    
    			foreach( $taxonomies as $category ) {
    				$hover= get_field('hover', 'term_' .$category->term_id  );
    				echo $hover;
    			}
    
    		endif;
    
    	endwhile;
    endif; wp_reset_query(); 
  • What code do you have?

    Maybe I’ve misunderstood.

    This loops categories:

    <?php
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );
     
    foreach( $categories as $category ) {
        $category_link = sprintf( 
            '<a href="%1$s" alt="%2$s">%3$s</a>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
            esc_html( $category->name )
        );
         
        echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
        echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
        echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
    } 

    This loops taxonomies:

    <?php if( $categories = get_terms( array( 'taxonomy' => 'floor4' ) ) ) :
    foreach( $categories as $cat) : 
    	$hover= get_field('hover', 'term_' .$cat->term_id  );?>
    	<?php echo $hover; ?>
    <?php endforeach;      
    endif;
    ?>
  • yes, echo $cat->term_id; returns correct value.
    ‘hover’ – is an acf field name, ‘floor4’ – a taxonomy which has a ‘hover’ value

  • if you echo $cat->term_id; does it show a value? If so, is it the right one?

    I assume ‘hover’ is the taxonomy name?

  • I’m a little lost by your question. Is the taxonomy selected using the standard WP taxonomy meta box or is the term only selected in an acf taxonomy field?

    If the term is selected using the standard wp taxonomy meta box then the value selected will be passed to your match function in the $screen arguments. Sorry, it has been a long time since I’ve created location rules and all of my examples use the old method.

    If there the term is set with an acf taxonomy field then it is more difficult because the values of other fields are not sent in the ajax request.

  • I’m not sure I explained well what I try to reach…

    To be sure :
    Products = post
    Categories = taxonomy

    When I go to this endpoint : https://mywebsite/wp-json/wc/v3/products/categories/
    I receive all the categories available for the products from the api.
    So there isn’t any filter. Screenshot enclose

    Before I will create a new category I need to know if the category already exist or not.

    I add a ACF field on the category to insert a unique ID.
    On the screenshot you can see coming from the plugin ACF-to-rest-api
    “acf”: {
    “id_category_mercator”: “merca1”
    },

    So the unique ID is “merca1”

    Now I want to ask the api to return me all the categories where there is “merca1” in the field “id_category_mercator”.
    Those values are stored in the table termmeta

    If it’s working, the api will return me 1 category and not all the list.

    Is it so more clear what I’m looking for or are your samples the correct ones ?

    Thanks in advance,

  • Ok I think I understand what you are trying to do, but correct my if I’m wrong.

    You want to retrieve TERMS from the taxonomy CATEGORY, but only those terms that have a specific value filled out in a custom field.

    There is an article in the ACF documentation describing how to do this if you were querying posts instead of taxonomy-terms: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    However, I believe the same principle can be applied to pre_get_terms (instead of pre_get_posts).
    The filter pre_get_terms is called inside WP_Term_Query so it should work for api requests: https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/

  • <div class="cat-extra-dis">
      <?php 
    		$queried_object = get_queried_object(); 
    		$taxonomy = $queried_object->taxonomy;
    		$term_id = $queried_object->term_id;  
    
            the_field('tekst_under_posts', $taxonomy . '_' . $term_id);
    	
    ?>
        </div>
        <!-- /.cat-extra-dis -->
  • Hello @jarvis,
    Thanks for your help.
    I use this code :

    function my_pre_get_products( $query ) {
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		return $query;
    	}
    	// only modify queries for 'product' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'product' ) {
    		// allow the url to alter the query
    		if( isset($_GET['id_article_mercator']) ) {
        		$query->set('meta_key', 'id_article_mercator');
    			$query->set('meta_value', $_GET['id_article_mercator']);
        	}
    	}
    	return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_products');

    Now I have a response on this url : https://mywebsite/wp-json/wc/v3/products?id_article_mercator=42771

    Now I want to have the same result for a category of the products.
    This url returns all the categories.
    https://mywebsite/wp-json/wc/v3/products/categories/

    I want to be able to filter a custom field added by ACF
    https://mywebsite/wp-json/wc/v3/products/categories/?myfield=testvalue

    Do you have an idea what to add as function for this ?
    What’s strange also, for the products, I can see in the rest api result the metadata with the values added by ACF
    For the category, the metadata aren’t visible.
    In the database the values are stored in termmeta for the categories and in postmeta for the products.
    If I’m right, the product is a post and the category is a taxonomy.

    Thanks in advance,

  • Yes, not impossible, but extremely (EXTREMELY) difficult to have archive pages based on ACf field values. If you need archive pages it is always better to use a taxonomy.

  • If each author can only have 1 country then it really does not matter. If you should want to list all authors in a specify country either a taxonomy or a custom field (single select) will perform just as well.

    If on the other hand each author could be listed in multiple countries then you are better off using a taxonomy field (custom taxonomy). Taxonomy queries are easier and perform better than querying for posts based on a multi select field. The reason for this is that a multi select stores values as a serialized array. Looking for one value is not a problem, but looking for posts that might match more than one value becomes complicated and the query performance degrades as you add more values to look for. See section 3 on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

Viewing 25 results - 876 through 900 (of 3,188 total)