Support

Account

Home Forums Backend Issues (wp-admin) Using the ACF Taxonomy to update the post's taxonomy

Solving

Using the ACF Taxonomy to update the post's taxonomy

  • Okay so I’m working on a complex project or a virtual artist alley kind of thing.

    we have custom post types for artists, crafters and writers, and we’re displaying the order of them based on a taxonomy of tier, so “sponsors” first, “shop keep” second etc. To do this I had to write a function that would update a custom field for an order number on save/update post. So if sponsor was selected as the term it will generate a 1 in the order field automatically. (this way we can sort the posts via this meta field, 1 first, 2 second etc). This was using a function that taps into the save_post function eg:

    add_action( 'save_post', 'myplugin_save_postdata' );
    
    /* When the post is saved, saves our custom data */
    function myplugin_save_postdata( $post_id ) {
    
       $post_id = get_the_ID();
       $post_type = get_post_type($post_id);
      // First we need to check if the current user is authorised to do this action. 
      //if ( 'page' == $_POST['artist_alley']) {
        //if ( ! current_user_can( 'edit_page', $post_id ) )
          //  return;
      //} else {
        //if ( ! current_user_can( 'edit_post', $post_id ) )
          //  return;
      //}
    
    //('artist_alley', 'writers_road', 'crafters_colonnade')
    
      if ($post_type == 'artist_alley' or $post_type == 'writers_road' or $post_type == 'crafters_colonnade') {
    
      $mydata = average_price(); // Do something with $mydata
      $order = tier_order();
    
      update_post_meta( $post_id, 'order', $order );
      update_post_meta( $post_id, 'average_price', $mydata );
      
      }
    }

    however there was some kind of delay with taxonomies doing it this way. It required the the post to be saved twice to update. So to work around this I created another custom field that was a taxonomy field and you can select the terms from that and that would populate the order field fine. But now my issue is the tier taxonomy needs to be selected twice. First for organiasing the posts and for search filters and the second time (through the acf taxonomy filed) to organise the display order. I’m wondering if there’s a way to use the acf taxonomy field to also update the posts taxonomy terms? So If I select or want to update an artist to second tier and do that through ACF can it also update the regular terms too. I tried a few options with save_post and wp_set_object_terms() but I just can’t seem to get it to work.

    `add_action(‘save_post’, ‘update_term’);
    function update_term( $post_id ) {
    if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) return;
    $post = get_post($post_id);
    if ($post_type == ‘artist_alley’ or $post_type == ‘writers_road’ or $post_type == ‘crafters_colonnade’) { // change ‘post’ to any cpt you want to target

    $term = get_field(‘tier’, $post_id);

    if($term) {
    $term_ID = $term->term_id;

    wp_set_object_terms($post_id, $term_ID, ‘tier’, true );
    }

    }
    }`

  • This is built into the ACF taxonomy field using the “Save Terms” and “Load Terms” setting.

  • To expand on the above, you would switch from using the “save_post” hook to the “acf/save_post” hook and run your other code based on the values in the ACF field.

  • Thanks, but still wouldn’t work. At first I got a ArgumentCountError and then i tried a fresh one and nothing

  • and the problem I had with the inbuilt feature of save and load was it still required to change the default wp taxonomy, it was ignoring/over riding the ACF field. The idea is so they only have to change it on the ACF field and it would automatically update the default one, but with the load and save terms it was doing it the other way arround.

  • You can remove the taxonomy meta box when creating registering your taxonomy by setting the ‘meta_box_cb’ argument to false. https://developer.wordpress.org/reference/functions/register_taxonomy/

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.