Support

Account

Home Forums ACF PRO Choose wordpress tags shown in ACF field taxonomy (= do not show every term)

Solved

Choose wordpress tags shown in ACF field taxonomy (= do not show every term)

  • Hello,

    I’m thinking about the possibility to sync certain acf checkbox values with the wordpress tags.

    My use case:
    I have two fields with checkboxes, one for architectural features and one for natural features of a certain point of interest. My aim is to link each term to an archive page where all posts that contain the specific term are shown. The terms are overlapping with the tags from WordPress, so to avoid building a parallel system with acf values (and a corresponding archive page), I thought about syncing them somehow the tags from WordPress. Of course I could achieve that without automatic syncing, but then I have to update each post with both acf values AND wordpress tags, so no thanks. Too much work and probably problems will arise in the future.

    I did some research and found the acf field for taxonomy. Now that already comes very close, but the problem here is, it simply shows me every tag. There doesn’t seem to be a way to limit the shown tags in the ACF UI, which is necessary in my case because I want to show the values for architectural and natural features in separate lists on the frontend.

    So, is there already a way to show only chosen wordpress tags in an acf taxonomy field? Didn’t found something in this regard yet. Or is this content for a suggestion?

    Thanks for your input!

  • ACF has a filter that will let you adjust the returned values shown for selection https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/

  • Hello John,

    thanks for your link!

    As a PHP beginner, I tried to use the code mentioned in the link in my theme’s function.php like this:

    function my_taxonomy_query( $args, $field, $post_id ) {
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
    
        // return
        return $args;
        }
    
    add_filter('acf/fields/taxonomy/query/name={$ti_tag_test}', 'my_taxonomy_query', 10, 3);

    At least I assume it’s for the theme’s functions.php. I also assume that the numbers 10 and 3 are for the tag IDs? I tried every variation for the name= – with and without {} and $, but it always shows every tag available in the post editor. Or is it not something so easy to implement?

    Yep, newbie here, sorry!

  • 
    "acf/fields/taxonomy/query/name={$field_name}"
    

    $field name is your field’s name so you would use

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

    10 in the example is the filter priority, this is the order that the filter runs in compared to all other filters for the same hook.

    3 is the number variables passed to your filter, in this case ($args, $field, $post_id)

    I really can’t give you specifics of what you need to add to $args because I don’t know how you want to determine what terms in the taxonomy shouldn’t be shown. What you really need to do is figure that out from the WP documentation for get_terms() https://developer.wordpress.org/reference/functions/get_terms/ which is linked to from the page I gave you the link to, as well as information on this page https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ that explains all of the arguments you can supply for get_terms()

  • Thanks a lot for your additional informations, much appreciated. I will look into it tomorrow, it’s late here.

    What I want to achieve is to show only certain tags that I’m gonna choose. Basically manually enter the tag IDs, we talk about 5-10 tags, not really a lot.

  • 
    $args['include'] = '1, 2, 3, 4'; // list of terms to include
    
  • Not really sure what I can still change to make it work. For now, I have the following in my theme’s functions.php:

    function my_taxonomy_query( $args, $field, $post_id ) {
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        $args['include'] = '589,186'; // list of terms to include
    
        // return
        return $args;
        }
    
    add_filter('acf/fields/taxonomy/query/name=ti_tag_test', 'my_taxonomy_query', 10, 3 );

    As far as I understand, the args value alters the get_terms function. I already defined which acf field I like to alter (add_filter, last row) and which tags to include with $args[‘include’]. It still shows all the tag terms though.

  • I’m on a dead-end here. I noticed from another coding issue that I should define the variables written in the function ($args, $field, $post_id). Post ID is clear what to do with it, but I’m not sure if and with what I should define $args and $fields. I have no idea what $fields relates to at all.

    $post_id = get_the_ID();
    function my_taxonomy_query( $args, $field, $post_id ) {
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        $args['include'] = '589,186'; // list of terms to include
        // return
        return $args;
        }
    add_filter('acf/fields/taxonomy/query/name=ti_tag_test', 'my_taxonomy_query', 10, 3 );
  • This is a filter as used in WP.

    The first argument of the filter is that value that you can adjust and return. In this case $args is an array of values that will be used when ACF calls get_terms(). Other values passed to your filter are for information. $field is an array with all of the setting of the current field that the filter is being run for and $post_id is the post ID of the current post being edited.

    I don’t know why the filter is not working for your field. Can you give me more information about the field? Is this taxonomy field a sub field of another field?

  • Thanks a lot.

    I got the meaning of $args already from your previous post.

    Currently it’s a top level field ‘taxonomy’, but in the end it’s supposed to be in a field group. I’ll attach some screenshots, German language though.

    NULL-Werte zulassen? = empty values allowed
    Eintr├Ąge laden = load entries

  • So the first thing you need to do is figure out if your filter is actually being called, which is a little complicated since it in a AJAX request.

    The first thing you need to to is turn on error logging https://codex.wordpress.org/WP_DEBUG

    Then at the top of the function you can output stuff to the error log, for example if you just want to see if the filter is called

    
    error_log('My Filter Was Called');
    

    Then you can look in the error log to see if that appears and that will tell you if the filter is called or not.

  • Ok, I added those line in wp-config.php:

    // Enable WP_DEBUG mode
    define( 'WP_DEBUG', true );
    
    // Enable Debug logging to the /wp-content/debug.log file
    define( 'WP_DEBUG_LOG', true );
    
    // Disable display of errors and warnings
    define( 'WP_DEBUG_DISPLAY', false );
    @ini_set( 'display_errors', 0 );

    I can see it’s working because the debug.log was created and is being filled with error messages.

    I added your error_log snippet directly under the function start:

    function my_taxonomy_query( $args, $field, $post_id ) {
      error_log('My Filter Was Called');
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        $args['include'] = '589,186'; // list of terms to include
        // return
        return $args;
        }
    add_filter('acf/fields/taxonomy/query/name=ti_tag_test', 'my_taxonomy_query', 10, 3 );

    Not sure about this position though.

    So far, nothing like ‘My Filter Was Called’ in the error_log.

  • If you’re not seeing that show up in the error log then your filter is not being called.

    In what file did you create the filter? functions.php?

  • Yes, it’s in the functions.php of my (child) theme.

  • The filter is not being called because you are using a checkbox field.

    When using a checkbox field ACF uses wp_list_categories() as well as a different filter that is documented there https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-wp_list_categories/

    Sorry for not catching this sooner.

  • Haha, there’s absolutely no reason for you to apologise. Thanks a lot for your continuous help, much appreciated! It’s working now.

    function my_taxonomy_wp_list_categories( $args, $field ) {
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        $args['include'] = '589,186'; // list of terms to include
        // return
        return $args;
    }
    add_filter('acf/fields/taxonomy/wp_list_categories/name=ti_tag_test', 'my_taxonomy_wp_list_categories', 10, 2);
  • Ok, that’s weird. Of course, I want to show the marked terms on my frontend. I used the code ‘Basic display (multiple terms)’ (https://www.advancedcustomfields.com/resources/taxonomy/) as it’s a checkbox, but no output is produced. It seems like the array is empty.

    My code:

    <p>Hier sollten die Tags stehen:
    <?php $ttterms = get_field('ti_tag_test');
      if( $ttterms ): ?>
        <?php foreach( $ttterms as $ttterm ): ?>
          <?php echo $ttterm->name; ?>
        <?php endforeach; ?>
      <?php endif; ?>
    </p>

    – I tried it with term object and term id.
    – …with $ttterm->name and $ttterm alone.
    – At least one term is checked.

    What am I missing?

Viewing 17 posts - 1 through 17 (of 17 total)

You must be logged in to reply to this topic.