Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • So I’m a little confused. You have a taxonomy field on a taxonomy. So when you’re editing a term in “podcast_country” you select a “podcast_topic”?

  • Thanks, but I’m trying to show the ‘Shows’ taxonomy items that have ‘podcast_topic’ applied to them. Not the posts.

  • You need to supply the term. See Adding fields to a taxonomy

    
    $queried_object = get_queried_object(); 
    ?>
    <a href="<?php bloginfo('url'); ?>/<?php the_field('page_link', $queried_object); ?>"><?php the_field('page_name', $queried_object); ?></a>
    
  • you should be able to use

    
    echo get_field('workspace_featured_image', $term);
    

    you can also use

    
    echo get_field('workspace_featured_image', 'term_'.$term->term_id);
    

    or even

    
    echo get_field('workspace_featured_image', $term->taxonomy.'_'.$term->term_id);
    

    This is all covered in the guide: Adding fields to a taxonomy term .

  • 
    // run function after acf updates fields
    add_action('acf/update_post', 20);
    function title_from_fields($post_id) {
      // check for your post type
      if (get_post_type($post_id) != 'job_updates')) {
        return;
      }
      // get the post
      $post = get_post($post_id);
      // get the taxonomy field
      $terms = get_field('job', $post_id);
      // taxonomy fields return an array, use the first term
      $term = $terms[0];
      // get date field
      $date = get_field('date', $post_id);
      // set post title/slug
      $title = $term->name.' '.$date;
      $post->post_title = $title;
      $post->post_name = sanitize_title($title);
      // remove this filter to prevent infinite loop
      remove_filter('acf/update_post', 20);
      // update the post
      wp_update_post($post);
      // re-add this filter
      add_action('acf/update_post', 20);
    }
    
  • old question popped to the top for spam.

    The code to show each term image:

    
    <?php 
    $terms = get_terms(array('taxonomy' => 'service', 'hide_empty' => false));
    foreach ($terms as $term) {
      $term_image = get_field( 'tax_image', $term);
      if ($term_image) {
        ?>
          <img src="<?php echo $term_image['url']; ?>" alt="<?php echo $term->name; ?>">
        <?php 
      }
    }
    ?>
    
  • You cannot merge the results of a post object and a taxonomy field. Posts and terms in WP are two different types of objects and handled completely differently.

    You need to look for a way to deal with the posts and terms separately.

  • Hi all, and John,

    I am looking to do something I think is this, but not totally sure: I need one field the user chooses one value from, that is a combination of posts of category “dogs”, and also of specific categories themselves, “dogs”, “cats”.

    It seems that the Post Object and Taxonomy are the right choices, now I just need to merge them into one select. This field will be on the options page.

    I am trying the code John shared before:

    $all = array_merge(
             get_field('relationship_1', false, false),
             get_field('relationship_2', false, false));
    $args = array(
      'post_type' => 'speakers',
      'posts_per_page' => -1,
      'post__in' => $all,
      'orderby' => 'title'
    )
    $speakers = new WP_Query($args);
    // post loop

    But, trying I’m unsure how to go from
    $speakers = new WP_Query($args);

    to the new field of merged fields – I get syntax error with the snippet in my functions.php. Do i need to instead assign the merged array $args into a new ACF select field? I probably create that field, and assign the array to it?

    The data will be used on many different posts, and archive pages, that’s why it’s in my ACF Options page.

    Thanks.

  • I guess I’m a newbie and didn’t realize that in a while loop you can run a get_row() to get the value of each loop.

    I found that when set to Taxonomy Terms and despite setting the “Return Value” to “Term object” get_row() only returns the term id. ( I guess I should change this back to ‘Term Id’ in case ACF fixes this bug in the future.)

    So I have to also run get_term($termid)

    Final result:

    $terms   = [];
    while ( have_rows( 'resources_carousel_topic_filter_list' ) ) : the_row();
       $terms[] = get_term(get_row());
    endwhile;
    
    echo "<pre>\n";
    var_dump( $terms );
    echo "</pre>\n";
  • I cannot find any examples of what you are looking for. This is not really something that people would try to do often because the taxonomy field can be set to allow adding terms and is how it would usually be done.

  • You need to use wp_insert_term() to add the new term to the taxonomy. Before you add the new term I would check to see if it exists using get_term_by() on the new term slug. After you insert the new term or get the existing term then you need to wp_set_object_terms().

  • Or should/could I maybe create a custom Taxonomy consisting of the car models and somehow inject that?

  • Asking the question in public is always the fastest way to find the answer yourself. πŸ˜‰ This needs some refinement, but setting the $field[‘default_value’] in a acf/load_field hooked function that runs after most other things is working:

    add_filter('acf/load_field/type=taxonomy', function( $field ) {
         
    	$screen = get_current_screen();
    	if ( $screen->base = 'term' && $screen->parent_base == 'edit' ) {
    		$term_id = (int) $_REQUEST['tag_ID'];
    	    $field['default_value'] = get_term_meta( $term_id, $field['name'] );
    	}
      
        return $field; 
        
    }, 99, 1);
  • displays term_taxonomy_id instead of term_id

  • I tried like this but it didn’t work for me πŸ™

    
    <?php 
    
    $category = get_the_terms($id, 'product_cat');
    $cater = $category[0]->term_id;
    //$taxonomy = 'product_cat';
    
    ?>
    
    <?php 
    if ( get_field( 'disable_finger', 'product_cat_' . $cater )):
    	?>
    <?php echo 'none';?>
    <?php else: ?>
           <div class="finger-size-wrap">
                        <select class="finger-size">
                            <option value="" selected="selected">Select finger size</option>
                            <?php for($i=3; $i<=10; $i=$i+0.5){
    						echo '<option value="'.$i.'">'.$i.'</option>';
    						}?>
                        </select>
                    </div>
    <?php endif; ?>
    
    
  • The post ID when getting values from a taxonomy should be 'term_'.$term->id

    A true false field returns either true or false, not yes or no.

    
    if (get_field( 'disable_finger', 'term_' . $cater )
       // field is set to true
    
  • Hello I would like to point out that after updating to the latest version of ‘Advanced Custom Fields’ we have experienced an excessive increase of memory usage by Apache PHP-FPM and by the database which has significantly slowed down our website. We’re not entirely sure it comes from ‘Advanced Custom Fields’ alone. But I created the log of the slow queries of the database and in all the slower ones they contain the term “acf-disabled” and they are like ‘SELECT SQL_CALC_FOUND_ROWS DISTINCT posts.ID FROM posts LEFT JOIN term_relationships estr ON (posts.ID = estr.object_id) LEFT JOIN term_taxonomy estt ON (estr.term_taxonomy_id = estt.term_taxonomy_id) LEFT JOIN terms est ON (estt.term_id = est.term_id) LEFT JOIN users esusers ON (posts.post_author = esusers.ID) WHERE 1 = 1 AND … and then contain (posts.post_type = ‘xxxxx’ AND (posts.post_status = ‘publish’ OR ales_posts.post_status = ‘acf-disabled’). We have a very large database that weighs over 1gb with over 100,000 rows in the post table and about 20,000 lines in the terms table. We run wordpress under CentOS 8 with PHP 7.4.29 and MariaDB 10.4.15.

    graph memory

    Here you can see the graph of the exponential increase in resources. The update of ‘Advanced Custom Fields’ was done on April 9th ​​along with that of WP and other plugins. Last night I worked on improving the situation but surely the website is still very slow.

    Thanks Marco

  • I’ve figured out a way to do this; I doubt it’s the most efficient method, but it works and that was my main objective. My code and explanation are for dynamically populating a post_object ACF field, but I think it would be a similar process for an ACF select field.

    I enqueue this Javascript code with the custom block to only load in the Admin:

    (function($){
    
        // the post_object ACF field in the custom ACF block that I want to filter
        let post_object_field_key = 'field_624f5d2c6b97c'; 
    
        // the ACF select field in the custom ACF block that I want to use to dynamically filter the ACF field above
        let region_selector_field_key = 'field_6259a73fcf5ef';
        let region_selector_field_name = 'my_region_selector';
    
        // Callback for the ACF select fields
        let selectFieldCallback = function( select_field ){
    
            // get the field name for the ACF field that was changed (I found this by using the Web Inspector and noticed that the ACF field name was saved as data attribute)
            let fieldName = select_field.$el[0].getAttribute('data-name');
    
            // clear the manually selected posts if the "my_region_selector" select field is changed
            if (fieldName === region_selector_field_name) {
    
                select_field.on('change', function() {
    
                    // the ID is in the form of the current ACF block instance ID + ACF field_key (eg 'acf-block_624f300b806d1-field_624f5d2c6b97c')
                    let this_el_id = select_field.$el.find('select')[0].id; 
    
                    // grab the first part of the ID which denotes the specific ACF block instance that this custom field is associated with
                    let block_el_id = this_el_id.split('-field_')[0];
    
                    // grab the post_object ACF field that is associated with the same ACF block instance
                    let adjacent_post_object_selector = document.getElementById(<code>${block_el_id}-${post_object_field_key}</code>);
                    
                    // calls a custom function that I wrote to clear the existing values that were selected in the post_object ACF field
                    clearSelectFieldValue(adjacent_post_object_selector);
    
                });
    
            }
    
        };
    
        // Set ACF actions for the block (I have multiple ACF select fields that I am using to filter one post_object field which is why I use new_field/type=select, but you could use a more specific filter)
        if ( window.acf ) {
            window.acf.addAction( 'new_field/type=select', selectFieldCallback );
        }
        
        // Create an ACF filter to dynamically populate the post_object ACF field with the appropriate posts based on what is selected in the relevant ACF Select fields
        acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ) {
    
            // since the 'select2_ajax_data' ACF filter can't be set for a specfic field, use this conditional to only filter the field we care about
            if ( data.field_key === post_object_field_key ) {
    
                // the ID is in the form of the current ACF block instance ID + ACF field_key (eg 'acf-block_624f300b806d1-field_624f5d2c6b97c')
                let this_el_id = instance.$el[0].id;
    
                // grab the first part of the ID which denotes the specific ACF block instance that this custom field is associated with
                let block_el_id = this_el_id.split('-field_')[0];
    
                // grab the ACF select field in the custom ACF block that I want to use to dynamically filter the post_object field
                let adjacent_region_selector = document.getElementById(<code>${block_el_id}-${region_selector_field_key}</code>);
                if (adjacent_region_selector) {
                    // My field allows multi-select so I use a custom function to grab the currently selected values of this multi-select field
                    let selectedRegions = getSelectedOptions(adjacent_region_selector);
                    if (selectedRegions.length) {
                        data.regions = selectedRegions; // send the currently selected post_type to the AJAX WP_Query when fetching results to populate the post_object custom field
                    }
                }
    
                // You can add more ACF fields that you want to use to filter the post_object field here using a similar format above
    
            }
            
            // 
            return data;
    
        });
    
    })(jQuery);
    

    Then I set up a PHP filter for the post_object field that I want to dynamically populate:
    add_filter( 'acf/fields/post_object/query/key=field_624f5d2c6b97c', 'dynamically_set_acf_post_object_query', 10, 3 );

    And here’s the corresponding function for the filter:

    <?php
    function dynamically_set_acf_post_object_query( $args, $field, $post_id ) {
    
        // if the AJAX request sends regions use them for a taxonomy query
        if (!empty($_POST['regions']) && is_array($_POST['regions'])) {
    
    		// if there are still region terms remaining in the array add a tax_query to the WP_Query arguments
    		if (count($filtered_regions) > 0) {
    			$args['tax_query'] = array(
    				'relation' => 'OR',
    				array(
    					'taxonomy'  => 'my_regions_taxonomy',
    		            'field'		=> 'slug',
    		            'terms'     => $_POST['regions'],
    				)
    			);
    		}
    
        }
        
        return $args;
    
    }
    
  • https://developer.wordpress.org/reference/functions/wp_set_post_term

    
    if($expiredate < $today){
    	wp_set_post_terms($p->ID, $term_id, $taxonomy, true);
    }
    
  • You will need to create a custom location rule.

    https://www.advancedcustomfields.com/resources/custom-location-rules/

    I don’t know anything about “bookable products”, is this a taxonomy? attribute?

    It can be quite difficult to create location rules for post settings outside of the standard WP settings.

  • This is not really an ACF question, it is a question about creating archive pages by in WP.

    When you load the URL for a CPT or a Term in a Taxonomy associated with that CPT WP has already done a query based on the CPT and the Term if applicable. Your query is overriding the query that WP already did.

    I would suggest the you look at pre_get_posts for and the WP template hierarchy rather than creating custom queries.

  • Yes my CPT has a taxonomy associated with it, i can use filter by myself when setting up the field

    But i was wondering if the USER themselves could select a category to filter with ? By typing it inside the field for exemple

  • If company name is a tag (taxonomy) then you should look at get_term_by(). Or simply set a taxonomy query to query posts in that tag.

  • To do this there must be a taxonomy associated with the CPT, either a built in taxonomy (like category) or a custom taxonomy. https://www.wpbeginner.com/wp-tutorials/how-to-add-categories-to-a-custom-post-type-in-wordpress/

  • You are not wrong. WP provides nowhere to edit content for the main taxonomy archive page = archive-{cpt-slug}.php

    The only way to accomplish this is to add your own admin page, like an ACF options page.

    Nor is there a place in the DB set aside to store this information, it must be stored in the options table.

    Or as an alternative as a “Page” with a custom page template.

    archive-{cpt-slug}.php could be used to load this custom page template, or the page template could be used to load archive-{cpt-slug}.php via get_template_part().

Viewing 25 results - 826 through 850 (of 3,193 total)