Support

Account

Forum Replies Created

  • Can you clarify what you want on the single page exactly?

    Do you want to see links to the other posts that are in the same Relationship field as the single post your viewing?

  • You need to pass the ID of the ‘p_image’ when calling the text field tied to it. So in your example:

    This: the_field('custom_pinterest_description')

    Should be updated to this: the_field('custom_pinterest_description', $image['ID'])

    That tells ACF to get the ‘custom_pinterest_description’ field linked to the image with that ID.

    Further examples and details can be found here: https://www.advancedcustomfields.com/resources/adding-fields-media-attachments

  • Yes, you can achieve this using the WordPress WP_Query and passing advanced custom fields to it with a meta query. Instructions on how to do this can be found here: https://www.advancedcustomfields.com/resources/query-posts-custom-fields

  • There are many ways to accomplish this, here’s one that will also put a comma after each name, except the final name.

    
    // Loop through all directors
    foreach ($team['Director'] as $director) {
    
        // Build an array of just the director names
        $names[] = $director["name"];
    
    }
    
    // Display a comma separated list of the names 
    echo implode(", ", $names);
    

    Hope that helps!

  • Fix for my “Update” issue above:

    In the issue above, Field 3 conditional settings weren’t working appropriately. The fields that were supposed to be hidden where indeed hidden, but the toggle to reveal them no longer revealed them.

    After many hours digging deep into ACF’s javascript and much trial and error, I finally found that calling acf.getField($field) is what is instantiating those fields properly. Why it isn’t working properly in my example, I’m not sure, but here’s the fix:

    /**
     *  Fires when duplicating a field from a previously unsaved, duplicated field.
     */
    
    // Only targeting select fields for this example
    acf.add_action('append_field/type=select', function ($field) {
    
      // Only target select fields that have my custom class
      if ($field.hasClass('my-custom-class')) {
    
        // Instantiate hidden sibling fields so conditionals and Select2 UI work
        acf.getFields($field.siblings('.my-custom-class.acf-hidden'))
    
      }
    
    })
  • Update:

    So my example above works great and it’s nice moving forward to know that conditions key is there when you need it.

    However, I’ve ran into an issue during testing where when I have a field with these settings, and I duplicate that field, it still works great, BUT… When I duplicate the field, and then duplicate the newly duplicated field, the newest field I duplicated doesn’t fire the show/hide from the conditionals.

    For example:
    Field 1 (saved in db) -> duplicated to Field 2 (not saved yet) [Field 2 conditionals work]
    Then: Field 2 (still unsaved) -> duplicated to Field 3 (also not saved yet) [Field 3 conditional don’t remove hidden attributes from the hidden fields when toggled to]

    I’ll post a solution if I find or create one. I may move a different direction on this project though. But just be aware if your using the conditions key.

  • I came across this same issue and while it can certainly be solved with custom javascript, I found another way that requires no extra js.

    I noticed that acf_render_field($field) for a custom field type, doesn’t initialize it into a select2 field with just 'ui' => 1 in the args.

    However acf_render_field_wrap($field) DOES automatically initialize select2 when the ui is set to true. The downside here is you have unnecessary nested field wrappers. So I kept digging and all it really boiled down to was needing a simple div with the necessary classes & data attributes. Then ACF is already looking for those and is the ui is also set to a true value… BOOM, select2 is initialized and works great!

    Here’s a stripped down code example of it working with a new custom field type:

    function render_field( $field ) {
    
    $field['type']     = 'select';
    $field['ui']       = 1;
    $field['choices']  = [
      'red'    => 'Red',
      'green'  => 'Green',
      'blue'   => 'Blue',
      'yellow' => 'Yellow',
    ];
    
    // Div with necessary classes/attributes to init Select2
    echo "<div class='acf-field acf-field-select' 
               data-name='{$field['label']}' 
               data-type='select' 
               data-key='{$field['key']}'>";
    
    acf_render_field($field);
    
    echo "</div>";
    
    }

    Hope that helps, good luck!

  • Thanks for the direction John.

    I ended up building this out from scratch, hiding the table rows with CSS visibility: collapse and then toggling them to visibility: visible with click events on the button group.

    The hardest part was digging through the core for an action to hook into, but I eventually found render_field_settings.

    But after your direction, I was like, duh… of course, there’s already toggles like on the radio and checkboxes you mentioned as well as the built in Conditional Logic toggle. So I took a look at their acf_render_field_settings and found the conditions key. I implemented that on my example and it works perfect, no extra JS or anything.

    Thank you for the reply and the direction, and all your help on this forum, you’re a rockstar!

    For anyone else, here’s a working example:

    
    // Show either the Button Text or Button Icon field setting
    acf_render_field_setting($field, [
        'label'        => __('Button Type', 'acf'),
        'instructions' => '',
        'type'         => 'button_group',
        'name'         => 'button_type',
        'layout'       => 'horizontal',
        'choices'      => [
            'text' => __('Text', 'acf'),
            'icon' => __('Icon', 'acf')
        ]
    ]);
    
    // Only show if Button Type field setting is set to Text
    acf_render_field_setting($field, [
        'label'        => __('Button Text', 'acf'),
        'instructions' => '',
        'type'         => 'text',
        'name'         => 'button_text',
        // Here's the magic
        'conditions'   => [
            'field'    => 'modal_button_type',
            'operator' => '==',
            'value'    => 'text'
        ]
    ]);
    
    // Only show if Button Type field setting is set to Icon
    acf_render_field_setting($field, [
        'label'        => __('Button Icon', 'acf'),
        'instructions' => 'Select an Icon',
        'type'         => 'text',
        'name'         => 'button_icon',
        // Here's the magic
        'conditions'   => [
            'field'    => 'modal_button_type',
            'operator' => '==',
            'value'    => 'icon'
        ]
    ]);
    
  • Solved!!!

    I dug into the ACF core for a while and after a ton of trial and error, I found a super simple way to do this.

    I created a custom endpoint that does the following:

    
    // Get new post's ID
    $post_id = $request['id'];
     
    // Get new post's meta fields
    $meta_fields = get_post_meta($post_id);
    
    // Save the meta fields to the post 
    acf_save_post($post_id, $meta_fields);
    
  • Thanks for the reply John. Through digging deeper, I’ve discovered that when creating a post that has ACF fields via the wp-api, for whatever reason none of those ACF fields are saved to the wp_postmeta table yet. It’s the “hard save” in the wp-admin panel that actually saves the ACF fields and stores them in the wp_postmeta table.

    I’ve tried to find a way to mimic that but have had no luck. If only there was a way to create a post that has ACF fields with the API, and then have those fields write to the wp_postmeta table upon creation. They just don’t save to the db until the “update” button is clicked 🙃

    Which is incredibly unfortunate when trying to use WordPress only as a Headless CMS, with no wp-admin interaction except via the API.

Viewing 10 posts - 26 through 35 (of 35 total)