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

collocating ACF fields to post title

Hi guys! I have multiple custom post types and for each post type I would like to auto-construct and update (if post is updated) the posts title and slug in slightly different ways. the post title should be built up out of 2-3 text fields.

I’ve noticed I need to do something with the acf/update_value filter and the the_title I understand the basics of the hooks needed, but this is getting to complex for me and I have no idea how to figure this out myself.

Thanks!

3 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hi,

What you're trying to do is a little complicated, especially if you haven't done it before.

I believe you will first have to tie into the wp_insert_post_data hook that fires before content is saved to the database. Then get the $post object, pull from it the data you need, reassign the title and slug values, and return the post object at the end of the function.

Boris Kamp
Boris Kamp
16,660 Points

Thank you Zac Gordon ! I will look into this and get back here if I need more help!

Boris Kamp
Boris Kamp
16,660 Points

Hi! I've almost done it except for how to pull the info from a relationship field:

//Auto add and update Title field:
    function my_post_title_updater( $post_id ) {

    if ( get_post_type( $post_id ) == 'reviews' ) {

        $relationship = get_field('target_product');

        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = get_field( 'kitName', $relationship->ID) ;

        // Update the post into the database
        wp_update_post( $my_post );

    }

}

the $relationship' variable should return an array of post objects soget_field( 'kitName', $relationship->ID)` should return the kitname of the product in the relationship field if you ask me...... right now it does not work..... if I use a field from the current post itself, it does work

I think I'm missing something, do you have any idea? When that is done, IO c

Thanks!

Boris Kamp
Boris Kamp
16,660 Points

Im still stuck with the question in my latest comment to my first post Zac Gordon. However, I have figured out how to use the post title as the slug. I did some more research and found this on the internet:

//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_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 );

This is called on posting a new post & on updating any existing post! It works great! Be sure to use the wp_unique_post_slug function as it makes sure the url is unique! if not it adds a number after it like test-2. Works like a charm!

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Glad you got it figured out! What is it you're still stuck with?

Boris Kamp
Boris Kamp
16,660 Points

Yeah! thanks for pointing me in the right direction! Im stuck with how to collocate the title field by using a relationship ACF field. When I finish that up, it automatically takes that as the slug and then we're done. here's some more info:

Right now I have this code to construct the post title by using ACF fields:

//Auto add and update Title field:
  function my_post_title_updater( $post_id ) {

    $my_post = array();
    $my_post['ID'] = $post_id;
    if ( get_post_type() == 'manufacturer' ) {
      $my_post['post_title'] = get_field('manufacturer_name');
    } elseif ( get_post_type() == 'products' ) {
      $my_post['post_title'] = get_field('kitName') . ' (' . ???? . get_field('kitNumber') . ')';
    }
    // Update the post into the database
    wp_update_post( $my_post );

  }

As you can see I've set it up for two post types, 'manufacturerandproducts` the first one is simple, just one ACF field. The second one though is more complicated, it has to be up out of three fields:

  1. 'kitName' (ACF text field )
  2. manufacturer (this is a relationship ACF field where you can select the manufacturer, this is a post in the manufacturer post type)
  3. 'kitNumber' (ACF text field)

so #1 & #3 are straight forward, and working, however for #2 I need it to get get_field('manufacturer_name'); from the post (manufacturer) chosen in the relationship.....

here is documentary on the relationship field : http://www.advancedcustomfields.com/resources/relationship/ They seem to use the foreach method every time, I don't need that though since there's only one one object So I tried something like this:

$manufacturer = get_field('manufacturer');
$my_post['post_title'] = get_field('kitName') . ' (' . get_field('manufacturer_name', $manufacturer->ID) . ' ' . get_field('kitNumber') . ')';

And that does not seem to do the trick...... Can you help me out with this?

Thanks!!