Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

auto update slug issue with ACF

I have created this function:

    //Auto update slug to be post title
    function myplugin_update_slug( $data, $postarr ) {
    if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) && !$data['post_type'] === 'acf-field-group' ) {
        $data['post_name'] = wp_unique_post_slug(sanitize_title( $data['post_title'] ), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent'] );
    }
    return $data;
    }
    add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );

In order to update my post's slug to be the post title. I set an iif statement to check the post status AND to prevent it from updating ACF field keys by using !$data['post_type'] === 'acf-field-group' However, when using this last part, the function is not working at all, leaving it out updates the post's slug but overwrites ACF field upon saving as well.

What's wrong in my statement? I can't figure it out.

Thanks!

I may be way off on this but how did you come up with "!$data['post_type'] === 'acf-field-group'" was that in their documentation somewhere? Wouldn't you want to find out the field using one of their functions and then test against that? I found this:

<?php
$field_name = "text_field";
$field = get_field_object($field_name);
?>

This may also help: http://support.advancedcustomfields.com/forums/topic/replacing-custom-post-type-post-title-with-an-acf/

Boris Kamp
Boris Kamp
16,660 Points

Sorry for my late reply! I found it on their forums as well, it was working correctly until some updates came trough! I don't get your question, this is not about fields right? it's about the post's slug? or am I completely misinterpreting your reply?